How to use ScottPlot to visualize data in .NET Polyglot notebooks

.NET Interactive Notebooks (polyglot notebooks) allow programmers to work with data in an interactive environment. To get started using ScottPlot to display data in a .NET notebook,

Create a new .NET Notebook

  • Install VS Code
  • Install the VS Code Polyglot Notebooks extension
  • Press CTRL + SHIFT + P to open the command dialogue
  • Select Polyglot Notebook: Create Default Notebook
  • Choose the .ipynb extension and select the C# language

Setup ScottPlot

Add this to the top of your notebook to use the ScottPlot NuGet package and make it easy to display plots inline:

// Install the ScottPlot NuGet package
#r "nuget:ScottPlot"

// Setup a custom formatter to display plots as images
using Microsoft.DotNet.Interactive.Formatting;
Formatter.Register(typeof(ScottPlot.Plot), (p, w) => 
    w.Write(((ScottPlot.Plot)p).GetImageHTML()), HtmlFormatter.MimeType);

Plot Data

// create sample data
double[] dataX = new double[] { 1, 2, 3, 4, 5 };
double[] dataY = new double[] { 1, 4, 9, 16, 25 };

// plot the data
var plt = new ScottPlot.Plot(400, 300);
plt.AddScatter(dataX, dataY);

// display the plot

Advanced Notebook Examples

Scatter plots, bar charts, and more are demonstrated in a notebook: