Skip to content

Getting Started

H.P. Gansevoort edited this page Nov 27, 2026 · 16 revisions

Getting Started

Installation

Pick the packages you need:

Package Command
Core (SVG + JSON) dotnet add package MatPlotLibNet
PNG & PDF export dotnet add package MatPlotLibNet.Skia
DataFrame integration dotnet add package MatPlotLibNet.DataFrame
Blazor components dotnet add package MatPlotLibNet.Blazor
ASP.NET Core / SignalR dotnet add package MatPlotLibNet.AspNetCore
Browser popup dotnet add package MatPlotLibNet.Interactive
GraphQL dotnet add package MatPlotLibNet.GraphQL
MAUI control dotnet add package MatPlotLibNet.Maui
Avalonia 12 control dotnet add package MatPlotLibNet.Avalonia
Uno Platform control dotnet add package MatPlotLibNet.Uno
WPF control dotnet add package MatPlotLibNet.Wpf
Geographic maps dotnet add package MatPlotLibNet.Geo
Polyglot / Jupyter #r "nuget: MatPlotLibNet.Notebooks"
MCP server (AI agents) dnx MatPlotLibNet.Mcp — see MCP Server

JavaScript (TypeScript and SignalR clients):

npm install @matplotlibnet/angular   # Angular
npm install @matplotlibnet/react     # React
npm install @matplotlibnet/vue       # Vue 3

More examples: the Cookbook has runnable code with rendered images for every chart type.


Your First Chart

using MatPlotLibNet;
using MatPlotLibNet.Styling;

double[] x = [1, 2, 3, 4, 5];
double[] y = [2, 4, 3, 5, 1];

Plt.Create()
    .WithTitle("My First Chart")
    .Plot(x, y, line => { line.Color = Colors.Blue; line.Label = "Data"; })
    .Save("chart.svg");

Save(path) detects the format from the file extension. You do not need to call Build() for simple file output.


Output Formats

using MatPlotLibNet;

double[] x = [1, 2, 3, 4, 5];
double[] y = [2, 4, 3, 5, 1];

Plt.Create().Plot(x, y).Save("chart.svg");    // SVG — built-in, zero dependencies
Plt.Create().Plot(x, y).Save("chart.json");   // JSON — portable, re-loadable
Plt.Create().Plot(x, y).Save("chart.png");    // PNG — requires MatPlotLibNet.Skia
Plt.Create().Plot(x, y).Save("chart.pdf");    // PDF — requires MatPlotLibNet.Skia

// Get strings directly
string svg  = Plt.Create().Plot(x, y).ToSvg();
string json = Plt.Create().Plot(x, y).ToJson();

Setting up PNG/PDF

MatPlotLibNet.Skia registers .png and .pdf itself when its assembly loads, which is the first time your code touches one of its types (figure.ToPng(), SkiaFonts, and so on). A program that only calls Save("chart.png") touches none, so register the Skia transforms once at startup. Without them Save("chart.png") and Save("chart.pdf") throw an InvalidOperationException that names the package; they never write SVG text under a raster name:

using MatPlotLibNet;
using MatPlotLibNet.Transforms;

FigureExtensions.RegisterTransform(".png", new PngTransform());
FigureExtensions.RegisterTransform(".pdf", new PdfTransform());

When You Need the Figure Object

To pass the figure to Blazor components, publishers, or servers, call .Build():

using MatPlotLibNet;
using MatPlotLibNet.Interactive;

double[] x = [1, 2, 3, 4, 5];
double[] y = [2, 4, 3, 5, 1];

var figure = Plt.Create().Plot(x, y).Build();

// Interactive browser popup
await figure.ShowAsync();

In an ASP.NET Core app, the publisher pushes the figure to every SignalR client that watches the chart:

using MatPlotLibNet;
using MatPlotLibNet.AspNetCore;
using Microsoft.AspNetCore.Builder;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMatPlotLibNetSignalR();
var app = builder.Build();
app.MapChartHub();

app.MapPost("/update", async (IChartPublisher publisher) =>
{
    var figure = Plt.Create().Plot([1.0, 2.0, 3.0], [2.0, 4.0, 3.0]).Build();
    await publisher.PublishSvgAsync("chart-1", figure);
});

app.Run();

In a Blazor component, the figure goes to the MplChart component:

<MplChart Figure="figure" />

Subplots

using MatPlotLibNet;

double[] x = [1, 2, 3, 4, 5];
double[] y = [2, 4, 3, 5, 1];

Plt.Create()
    .WithSize(1200, 600)
    .AddSubPlot(1, 2, 1, ax => ax.WithTitle("Line").Plot(x, y))
    .AddSubPlot(1, 2, 2, ax => ax.WithTitle("Bar").Bar(["A", "B", "C"], [10, 20, 15]))
    .Save("subplots.svg");

Legend

using MatPlotLibNet;
using MatPlotLibNet.Models;

double[] x        = [1, 2, 3, 4, 5];
double[] temp     = [22.1, 23.4, 21.8, 24.2, 22.9];
double[] humidity = [61.0, 58.0, 65.0, 60.0, 63.0];

Plt.Create()
    .AddSubPlot(1, 1, 1, ax => ax
        .Plot(x, temp,     s => s.Label = "Temperature")
        .Plot(x, humidity, s => s.Label = "Humidity")
        .WithLegend(LegendPosition.UpperRight))
    .Save("legend.svg");

DataFrame Package

Plot, compute indicators, and run regressions directly from Microsoft.Data.Analysis.DataFrame column names:

dotnet add package MatPlotLibNet.DataFrame
using MatPlotLibNet;
using MatPlotLibNet.Indicators;
using Microsoft.Data.Analysis;

// 30 rows: two tickers of 15 days each
double[] row = [.. Enumerable.Range(0, 30).Select(i => (double)i)];
double[] close = [.. row.Select(i => 100 + 5 * Math.Sin(i / 4) + i * 0.3)];

var df = new DataFrame(
    new PrimitiveDataFrameColumn<double>("date", [.. row.Select(i => i % 15 + 1)]),
    new PrimitiveDataFrameColumn<double>("price", close),
    new StringDataFrameColumn("ticker", [.. row.Select(i => i < 15 ? "AAA" : "BBB")]),
    new PrimitiveDataFrameColumn<double>("close", close),
    new PrimitiveDataFrameColumn<double>("x", row),
    new PrimitiveDataFrameColumn<double>("y", [.. row.Select(i => i * i / 10 + i % 3)]));
double[] evalX = [0, 10, 20, 29];

// Line/scatter/histogram from named columns
df.Line(x: "date", y: "price", hue: "ticker").Save("prices.svg");
df.Scatter(x: "x", y: "y", hue: "ticker").Save("points.svg");

// Technical indicators: the indicator's own class over the column's values
double[] sma   = new Sma(df.Values("close"), 20).Compute();
double[] rsi   = new Rsi(df.Values("close"), 14).Compute();
var      bands = new BollingerBands(df.Values("close"), 20).Compute();

// Polynomial regression + confidence band
double[] coefs = df.PolyFit("x", "y", degree: 2);
var      ci    = df.ConfidenceBand("x", "y", coefs, evalX);

Prerequisites

  • .NET 10 SDK (or .NET 8 for MatPlotLibNet and MatPlotLibNet.Skia)
  • Visual Studio 2022, VS Code, or JetBrains Rider

Common Issues

Problem Cause Fix
SVG renders blank / axes only Data arrays empty, all-NaN, or X range too narrow Check your data before plotting; verify x.Length > 0
Save("chart.png") or Save("chart.pdf") throws InvalidOperationException ("needs the exporters of MatPlotLibNet.Skia, which is not loaded") MatPlotLibNet.Skia not installed, or no member of it used and its transforms not registered dotnet add package MatPlotLibNet.Skia and register transforms (see above)
Legend labels missing Series added without .Label Set s.Label = "..." in the configure lambda
Subplots overlap No spacing configured Add .TightLayout() or .WithSubPlotSpacing(...)
ShowAsync() hangs MatPlotLibNet.Interactive not installed dotnet add package MatPlotLibNet.Interactive

MatPlotLibNet 1.30.0

On the docs site

Clone this wiki locally