---
lang: ja-jp
breaks: true
---
# `Plotly.NET` のサンプル 2023-02-21
https://plotly.com/csharp/
## Hello from C#
```xml=
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Plotly.NET" Version="3.0.1" />
<PackageReference Include="Plotly.NET.CSharp" Version="0.8.0" />
</ItemGroup>
```
```csharp=
using Plotly.NET.CSharp;
```
```csharp=
Chart.Point<double, double, string>(
x: new double[] { 1, 2 },
y: new double[] { 5, 10 }
)
.WithTraceInfo("Hello from C#", ShowLegend: true)
.WithXAxisStyle<double, double, string>(Title: Plotly.NET.Title.init("xAxis"))
.WithYAxisStyle<double, double, string>(Title: Plotly.NET.Title.init("yAxis"))
.SaveHtml(@"..\..\..\html\001_Hello_from_C#.html", OpenInBrowser: true);
```

## Basic Bar Chart
```csharp=
var animals = new[] { "giraffes", "orangutans", "monkeys" };
var sfValues = new[] { 20, 14, 23 };
Chart.Column<int, string, string>(sfValues, animals)
.SaveHtml(@"..\..\..\html\002_Basic_Bar_Chart.html", OpenInBrowser: true);
```

## Grouped Bar Chart
```csharp=
var animals = new[] { "giraffes", "orangutans", "monkeys" };
var sfValues = new[] { 20, 14, 23 };
var laValues = new[] { 12, 18, 29 };
Chart.Combine(new[] {
Chart.Column<int, string, string>(sfValues, animals ,"SF Zoo"),
Chart.Column<int, string, string>(laValues, animals , "LA Zoo")
})
.SaveHtml(@"..\..\..\html\003_Grouped_Bar_Chart.html", OpenInBrowser: true);
```

## Stacked Bar Chart
```csharp=
var animals = new[] { "giraffes", "orangutans", "monkeys" };
var sfValues = new[] { 20, 14, 23 };
var laValues = new[] { 12, 18, 29 };
Chart.Combine(new[]
{
Chart.StackedColumn<int, string, string>(sfValues, animals, "SF Zoo"),
Chart.StackedColumn<int, string, string>(laValues, animals, "LA Zoo"),
})
.SaveHtml(@"..\..\..\html\004_Stacked_Bar_Chart.html", OpenInBrowser: true);
```

## Creating Figures using DynamicObject
```csharp=
using Microsoft.FSharp.Collections;
using Plotly.NET;
using Plotly.NET.LayoutObjects;
```
```csharp=
LinearAxis xAxis = new LinearAxis();
xAxis.SetValue("title", "xAxis");
xAxis.SetValue("zerolinecolor", "#ffff");
xAxis.SetValue("gridcolor", "#ffff");
xAxis.SetValue("showline", true);
xAxis.SetValue("zerolinewidth", 2);
LinearAxis yAxis = new LinearAxis();
yAxis.SetValue("title", "yAxis");
yAxis.SetValue("zerolinecolor", "#ffff");
yAxis.SetValue("gridcolor", "#ffff");
yAxis.SetValue("showline", true);
yAxis.SetValue("zerolinewidth", 2);
Layout layout = new();
layout.SetValue("xaxis", xAxis);
layout.SetValue("yaxis", yAxis);
layout.SetValue("title", "A Figure Specified by DynamicObj");
layout.SetValue("plot_bgcolor", "#e5ecf6");
layout.SetValue("showlegend", true);
Trace trace = new("bar");
trace.SetValue("x", new[] { 1, 2, 3 });
trace.SetValue("y", new[] { 1, 3, 2 });
var fig = GenericChart.Figure.create(ListModule.OfSeq(new[] { trace }), layout);
GenericChart.fromFigure(fig)
.SaveHtml(@"..\..\..\html\005_Creating_Figures_using_DynamicObject.html", OpenInBrowser: true);
```

## Figures as GenericChart Objects
```csharp=
using Plotly.NET;
using Plotly.NET.LayoutObjects;
```
```csharp=
var xAxis = LinearAxis.init<IConvertible, IConvertible, IConvertible, IConvertible, IConvertible, IConvertible>(
Title: Title.init("xAxis"),
ZeroLineColor: Color.fromString("#ffff"),
GridColor: Color.fromString("#ffff"),
ZeroLineWidth: 2);
var yAxis = LinearAxis.init<IConvertible, IConvertible, IConvertible, IConvertible, IConvertible, IConvertible>(
Title: Title.init("yAxis"),
ZeroLineColor: Color.fromString("#ffff"),
GridColor: Color.fromString("#ffff"),
ZeroLineWidth: 2);
var layout = Layout.init<IConvertible>(Title: Title.init("A Plotly.NET Chart"), PlotBGColor: Color.fromString("#e5ecf6"));
Plotly.NET.CSharp.Chart.Column<int, int, int>(
values: new[] { 1, 3, 2 },
Keys: new[] { 1, 2, 3 }
)
.WithXAxis(xAxis)
.WithYAxis(yAxis)
.WithLayout(layout)
.SaveHtml(@"..\..\..\html\006_Figures_as_GenericChart_Objects.html", OpenInBrowser: true);
```

## Scatter Plot
```xml=
<ItemGroup>
<PackageReference Include="FSharp.Data" Version="5.0.2" />
<PackageReference Include="Plotly.NET" Version="3.0.1" />
<PackageReference Include="Plotly.NET.CSharp" Version="0.8.0" />
</ItemGroup>
```
```csharp=
using FSharp.Data;
using Plotly.NET;
using Plotly.NET.LayoutObjects;
```
```csharp=
var title = Title.init(Text: "A Plotly Figure");
var csv = CsvFile.Load("https://raw.githubusercontent.com/plotly/datasets/master/iris.csv");
IEnumerable<string> GetData(string column) => csv.Rows.Select(row => row.GetColumn(column));
var X = Array.ConvertAll(GetData("SepalWidth").ToArray(), Single.Parse);
var Y = Array.ConvertAll(GetData("SepalLength").ToArray(), Single.Parse);
var Colors = GetData("Name")
.Select(name =>
name switch {
"Iris-setosa" => Color.fromString("blue"),
"Iris-versicolor" => Color.fromString("orange"),
_ => Color.fromString("deeppink")
});
var layout = Layout.init<IConvertible>(Title: title, PlotBGColor: Color.fromString("#e5ecf6"));
var xAxis = LinearAxis.init<IConvertible, IConvertible, IConvertible, IConvertible, IConvertible, IConvertible>(
Title: Title.init("xAxis"),
ZeroLineColor: Color.fromString("#ffff"),
GridColor: Color.fromString("#ffff"),
ZeroLineWidth: 2);
var yAxis = LinearAxis.init<IConvertible, IConvertible, IConvertible, IConvertible, IConvertible, IConvertible>(
Title: Title.init("yAxis"),
ZeroLineColor: Color.fromString("#ffff"),
GridColor: Color.fromString("#ffff"),
ZeroLineWidth: 2);
Plotly.NET.CSharp.Chart.Scatter<float, float, string>(
x: X,
y: Y,
mode: StyleParam.Mode.Markers,
MarkerColor: Color.fromColors(Colors)
)
.WithLayout(layout)
.WithXAxis(xAxis)
.WithYAxis(yAxis)
.SaveHtml(@"..\..\..\html\007_Scatter_Plot.html", OpenInBrowser: true);
```

## Make Subplots
```csharp=
using Plotly.NET;
```
```csharp=
int[] x = { 1, 2, 3 };
int[] y = { 4, 2, 1 };
var scatter = Plotly.NET.CSharp.Chart.Scatter<int, int, string>(
x: x,
y: y,
mode: StyleParam.Mode.Markers,
Name: "Scatter"
);
var bar = Plotly.NET.CSharp.Chart.Column<int, int, int>(
values: y,
Keys: x,
Name: "Bar"
);
var chartCombined = new[] { scatter, bar };
Chart.Grid<IEnumerable<GenericChart.GenericChart>>(1, 2)
.Invoke(chartCombined)
.SaveHtml(@"..\..\..\html\008_Make_Subplots.html", OpenInBrowser: true);
```

## Adding Traces
```csharp=
int[] x = { 1, 2, 3 };
int[] y = { 4, 2, 1 };
var scatter = Plotly.NET.CSharp.Chart.Scatter<int, int, string>(x: x, y: y, mode: StyleParam.Mode.Markers, Name: "Scatter")
.WithMarkerStyle(Size: 10);
var bar = Plotly.NET.CSharp.Chart.Column<int, int, int>(values: y, Keys: x, Name: "Bar");
Chart.Combine(new[] { scatter, bar })
.SaveHtml(@"..\..\..\html\009_Adding_Traces.html", OpenInBrowser: true);
```

## Updating Figure Layouts
```csharp=
var title = Title.init(Text: "Using Layout() With Plotly Figures");
var layout = Layout.init<IConvertible>(Title: title,
PlotBGColor: Color.fromString("#e5ecf6"),
Font: Font.init(Size: 17));
Plotly.NET.CSharp.Chart.Column<int, int, int>(values: new[] { 4, 2, 1 }, Keys: new[] { 1, 2, 3 })
.WithLayout(layout)
.SaveHtml(@"..\..\..\html\010_Updating_Figure_Layouts.html", OpenInBrowser: true);
```

## Updating Traces
```csharp=
var fig1 = new[] {
Plotly.NET.CSharp.Chart.Column<int,int,int>(
Keys:new []{0,1,2},
values:new []{2,1,3},
Name:"b",
MarkerColor:Color.fromString("red")
),
Plotly.NET.CSharp.Chart.Scatter<int,double,int>(
x:new []{0,1,2},
y:new[]{4,2,3.5},
Name:"a",
mode:StyleParam.Mode.Markers
)
.WithMarkerStyle(
Size:20,
Color:Color.fromString("rgb(51, 204, 51)")
)
};
var combinedFig1 = Chart.Combine(fig1);
var fig2 = new[] {
Plotly.NET.CSharp.Chart.Column<int,int,int>(
Keys:new []{0,1,2},
values:new []{1,3,2},
Name:"c",
MarkerColor:Color.fromString("#33cc33")
),
Plotly.NET.CSharp.Chart.Scatter<int,double,int>(
x:new []{0,1,2},
y:new[]{2,3.5,4},
Name:"a",
mode:StyleParam.Mode.Markers
)
.WithMarkerStyle(
Size:20,
Color:Color.fromString("rgb(255, 0,0)")
)
};
var combinedFig2 = Chart.Combine(fig2);
Chart.Grid<IEnumerable<GenericChart.GenericChart>>(1, 2)
.Invoke(new[] { combinedFig1, combinedFig2 })
.SaveHtml(@"..\..\..\html\011_Updating_Traces.html", OpenInBrowser: true);
```

```csharp=
var fig1 = new[]{
Plotly.NET.CSharp.Chart.Column<int,int,int>(
Keys:new []{0,1,2},
values:new []{2,1,3},
Name:"b",
MarkerColor:Color.fromString("red")
),
Plotly.NET.CSharp.Chart.Scatter<int,double,int>(
x:new []{0,1,2},
y:new[]{4,2,3.5},
Name:"a",
mode:StyleParam.Mode.Markers
)
.WithMarkerStyle(
Size:20,
Color:Color.fromString("rgb(51, 204, 51)")
)
};
var combinedFig1 = Chart.Combine(fig1)
.WithMarkerStyle(Size: 20, Color: Color.fromString("blue"));
var fig2 = new[]{
Plotly.NET.CSharp.Chart.Column<int,int,int>(
Keys:new []{0,1,2},
values:new []{1,3,2},
Name:"c",
MarkerColor:Color.fromString("#33cc33")
),
Plotly.NET.CSharp.Chart.Scatter<int,double,int>(
x:new []{0,1,2},
y:new[]{2,3.5,4},
Name:"a",
mode:StyleParam.Mode.Markers
)
.WithMarkerStyle(
Size:20,
Color:Color.fromString("rgb(255, 0,0)")
)
};
var combinedFig2 = Chart.Combine(fig2)
.WithMarkerStyle(
Size: 20,
Color: Color.fromString("blue")
);
Chart.Grid<IEnumerable<GenericChart.GenericChart>>(1, 2)
.Invoke(new[] { combinedFig1, combinedFig2 })
.SaveHtml(@"..\..\..\html\012_Updating_Traces.html", OpenInBrowser: true);
```

## Overwrite Existing Properties
```csharp=
var marker = Marker.init(Opacity: 0.4);
Plotly.NET.CSharp.Chart.Column<int, int, int>(
Keys: new[] { 1, 2, 3 },
values: new[] { 1, 3, 2 },
MarkerColor: Color.fromString("red")
)
.WithMarker(marker)
.SaveHtml(@"..\..\..\html\013_Overwrite_Existing_Properties.html", OpenInBrowser: true);
```

## Updating Figure Axes
```csharp=
var rand = new Random();
var x = Enumerable.Range(1, 100).ToArray();
var y1 = x.Select(_ => rand.NextDouble() * 10 + 5);
var y2 = x.Select(_ => rand.NextDouble() * 5);
var subPlots = new[]{
new []{
new Tuple<StyleParam.LinearAxisId, StyleParam.LinearAxisId>(
StyleParam.LinearAxisId.X.NewX(1),
StyleParam.LinearAxisId.X.NewY(1)
),
new Tuple<StyleParam.LinearAxisId, StyleParam.LinearAxisId>(
StyleParam.LinearAxisId.X.NewX(2),
StyleParam.LinearAxisId.X.NewY(1)
)
}
};
var layoutGrid = LayoutGrid.init(Rows: 1, Columns: 2, SubPlots: subPlots);
var chart1 = Plotly.NET.CSharp.Chart.Point<int, double, int>(x: x, y: y1, Name: "1,1")
.WithAxisAnchor(X: 1);
var chart2 = Plotly.NET.CSharp.Chart.Point<int, double, int>(x: x, y: y2, Name: "1,2")
.WithAxisAnchor(X: 2);
Chart.Combine(new[] { chart1, chart2 })
.WithLayoutGrid(layoutGrid)
.WithXAxisStyle(title: Title.init(), ShowGrid: false, Id: StyleParam.SubPlotId.NewXAxis(1))
.WithXAxisStyle(title: Title.init(), ShowGrid: false, Id: StyleParam.SubPlotId.NewXAxis(2))
.SaveHtml(@"..\..\..\html\014_Updating_Figure_Axes.html", OpenInBrowser: true);
```

## Chaining Figure Operations
```csharp=
var marker = Marker.init(
Size: 20,
Color: Color.fromString("yellow"),
Outline: Line.init(Width: 1.5)
);
var fig1 = new[]{
Plotly.NET.CSharp.Chart.Column<int,int,int>(
Keys:new []{0,1,2},
values:new []{2,1,3},
Name:"b",
MarkerColor:Color.fromString("red")
)
.WithMarker(marker),
Plotly.NET.CSharp.Chart.Scatter<int,double,int>(
x:new []{0,1,2},
y:new[]{4,2,3.5},
Name:"a",
mode:StyleParam.Mode.Markers
)
.WithMarkerStyle(Size:20,Color:Color.fromString("blue"))
};
Chart.Combine(fig1)
.WithTitle(title: "Chaining Multiple Chart Operations With A Plotly Chart", TitleFont: Font.init(Size: 15))
.WithXAxisStyle(title: Title.init(), ShowGrid: false)
.SaveHtml(@"..\..\..\html\015_Chaining_Figure_Operations.html", OpenInBrowser: true);
```

## Property Assignment
```csharp=
var layout = new Layout();
layout.SetValue("title", "Using Property Assignment Syntax With GenericChart Object");
layout.SetValue("plot_bgcolor", "#e5ecf6");
layout.SetValue("showlegend", true);
Plotly.NET.CSharp.Chart.Column<int, int, int>(
Keys: new[] { 1, 2, 3 },
values: new[] { 1, 3, 2 },
MarkerColor: Color.fromString("red")
)
.WithLayout(layout)
.SaveHtml(@"..\..\..\html\016_Property_Assignment.html", OpenInBrowser: true);
```

```csharp=
var marker = new Marker();
var line = new Line();
line.SetValue("width", 4);
line.SetValue("color", "black");
marker.SetValue("line", line);
Plotly.NET.CSharp.Chart.Column<int, int, int>(
Keys: new[] { 1, 2, 3 },
values: new[] { 1, 3, 2 },
MarkerColor: Color.fromString("red")
)
.WithMarker(marker)
.SaveHtml(@"..\..\..\html\017_Property_Assignment.html", OpenInBrowser: true);
```

###### tags: `Plotly.NET` `サンプル`