---
# System prepended metadata

title: '`Visual Studio Code` で `Polyglot Notebooks`（`.NET Interactive Notebooks`）　を使用する。※`ScottPlot.NET`を使用した可視化 2023-02-20'
tags: [Visual Studio Code, ScottPlot.NET, Polyglot Notebooks, DataFrame, .NET Interactive Notebooks]

---

---
lang: ja-jp
breaks: true
---
# `Visual Studio Code` で `Polyglot Notebooks`（`.NET Interactive Notebooks`）　を使用する。※`ScottPlot.NET`を使用した可視化 2023-02-20

> .NET Interactive Notebooks is now Polyglot Notebooks!
> https://devblogs.microsoft.com/dotnet/dotnet-interactive-notebooks-is-now-polyglot-notebooks/

## Visual Studio Codeに `Polyglot Notebooks` 拡張機能を追加する。

![](https://i.imgur.com/jkl31DO.png)

![](https://i.imgur.com/RLlhwYD.png)


## コマンドパレットより、空のノートブックを追加する

![](https://i.imgur.com/e8tj9Zv.png)

![](https://i.imgur.com/wjaORDR.png)

![](https://i.imgur.com/ewUloe0.png)

![](https://i.imgur.com/guABLDa.png)

## 使用してみる

> Using DataFrames in Interactive Notebooks
> https://swharden.com/blog/2022-05-01-dotnet-dataframe/#using-dataframes-in-interactive-notebooks

> swharden/Csharp-Data-Visualization
> https://github.com/swharden/Csharp-Data-Visualization/tree/main/projects/dataframe


```csharp=
#r "nuget:Microsoft.Data.Analysis"
using Microsoft.Data.Analysis;
```

![](https://i.imgur.com/aB6fCMn.png)

`Ctrl` + `Alt` + `Enter` で実行

![](https://i.imgur.com/EuZOD94.png)

ノートブックの空いているところで、「＋コード」をクリックし、コードセルを追加する。

![](https://i.imgur.com/7yEvAUJ.png)


![](https://i.imgur.com/byrEd3t.png)

```csharp=
// load sample data
string[] names = { "Oliver", "Charlotte", "Henry", "Amelia", "Owen" };
int[] ages = { 23, 19, 42, 64, 35 };
double[] heights = { 1.91, 1.62, 1.72, 1.57, 1.85 };

DataFrameColumn[] columns = {
    new StringDataFrameColumn("Name", names),
    new PrimitiveDataFrameColumn<int>("Age", ages),
    new PrimitiveDataFrameColumn<double>("Height", heights),
};

DataFrame df = new(columns);
```

![](https://i.imgur.com/QwAE0wr.png)


```csharp=
// visualize the dataframe
df
```

![](https://i.imgur.com/csfoaej.png)


```csharp=
// install ScottPlot and register its display type
#r "nuget:ScottPlot"
using Microsoft.DotNet.Interactive.Formatting;
Formatter.Register(typeof(ScottPlot.Plot), (plt, writer) => 
    writer.Write(((ScottPlot.Plot)plt).GetImageHTML()), HtmlFormatter.MimeType);
```

![](https://i.imgur.com/HHgqVrI.png)

```csharp=
// get data as double arrays
double[] ages = Enumerable.Range(0, (int)df.Rows.Count).Select(x => Convert.ToDouble(df["Age"][x])).ToArray();
double[] heights = Enumerable.Range(0, (int)df.Rows.Count).Select(x => Convert.ToDouble(df["Height"][x])).ToArray();

// create and display a plot
var plt = new ScottPlot.Plot(400, 300);
plt.AddScatter(ages, heights);
plt.XLabel("Age");
plt.YLabel("Height");
plt
```

![](https://i.imgur.com/FUPYK7u.png)


###### tags: `Visual Studio Code` `.NET Interactive Notebooks` `Polyglot Notebooks` `DataFrame` `ScottPlot.NET`
