⚠️ Warning: This website is still being developed. Code examples, videos, and links may be broken while I continue to work on it. -- Scott, May 10, 2020
Resources for visualizing data using C# and the .NET platform

Plot Data with NPlot

NPlot is a free charting library for .NET developed by Matt Howlett and others. It can be used to render graphs as Bitmaps (suitable for use in console applications) and it has a mouse-interactive user control for Windows Forms. While NPlot is simple, it lacks many features, targets .NET Framework 4.0, and has not been actively developed for many years.

Code

  • Get the NPlot package using NuGet
  • Drag a PlotSurface2D from the toolbox onto your form

Sample Data

This code generates random data we can practice plotting

private Random rand = new Random(0);
private double[] RandomWalk(int points = 5, double start = 100, double mult = 50)
{
    // return an array of difting random numbers
    double[] values = new double[points];
    values[0] = start;
    for (int i = 1; i < points; i++)
        values[i] = values[i - 1] + (rand.NextDouble() - .5) * mult;
    return values;
}

Interactive Line Plot

I found I could display lines of 10,000 points, but by 100,000 points the program became very sluggish.

// generate some random Y data
int pointCount = 10_000;
double[] ys1 = RandomWalk(pointCount);
double[] ys2 = RandomWalk(pointCount);
double[] ys3 = RandomWalk(pointCount);

// create a line plot containing the data
var linePlot1 = new NPlot.LinePlot { DataSource = ys1, Color = Color.Red };
var linePlot2 = new NPlot.LinePlot { DataSource = ys2, Color = Color.Green };
var linePlot3 = new NPlot.LinePlot { DataSource = ys3, Color = Color.Blue };

// add the line plot to the plot surface (user control)
plotSurface2D1.Clear();
plotSurface2D1.Add(linePlot1);
plotSurface2D1.Add(linePlot2);
plotSurface2D1.Add(linePlot3);
plotSurface2D1.Title = $"Line Plot ({pointCount:n0} points each)";
plotSurface2D1.YAxis1.Label = "Vertical Axis Label";
plotSurface2D1.XAxis1.Label = "Horizontal Axis Label";
plotSurface2D1.Refresh();

// allow the plot to be mouse-interactive
plotSurface2D1.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag());
plotSurface2D1.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.VerticalDrag());
plotSurface2D1.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.AxisDrag(true));

Console Application Support

Users who aren't using Windows Forms (or the PlotSurface2D user control) can render directly on Bitmap objects using the Bitmap.PlotSurface2D class. This allows NPlot to be used in console applications (or in server applications such as ASP.NET)

var linePlot = new NPlot.PointPlot { DataSource = RandomWalk(20) };
var surface = new NPlot.Bitmap.PlotSurface2D(400, 300);
surface.BackColor = Color.White;
surface.Add(linePlot);
surface.Title = $"Scatter Plot from a Console Application";
surface.YAxis1.Label = "Vertical Axis Label";
surface.XAxis1.Label = "Horizontal Axis Label";
surface.Refresh();
surface.Bitmap.Save("nplot-console-quickstart.png");

WPF Support

NPlot is designed for Windows Forms, but the author noted it can be used in WPF applications too. This seems to be accomplished by rendering plots to Bitmap objects and displaying them in WPF. WChart is a modified version of NPlot designed for WPF, but it hasn't been updated since 2015.

Resources

Source Code