Simple 2D (X,Y) Plots in C#
If you are using C# for a scientific computing project you might have run into trouble trying to find a simple 2D plot library. Below is an explanation of how to quickly make a simple library that you can use from another application to plot data from C#.
I'm assuming you already have a solution open in Visual Studio, and that this solution contains some C# code with data you'd like to plot. Now follow these steps to get the data into a plot.
I'm assuming you already have a solution open in Visual Studio, and that this solution contains some C# code with data you'd like to plot. Now follow these steps to get the data into a plot.
- Make a new project in Visual Studio
- File -> New -> Project
- Select Windows Form Application (we'll switch from an application to a library soon)
- In the "Solution:" box at the bottom select "Add to solution" and click OK
- A window with Form1.cs should open
- Add a Chart object to this project.
- Click View -> Toolbox
- Within the toolbox Windows select Data->Chart (if the chart option is not available in the toolbox select "choose items" and then, from .net framework components, select System.Web.UI.DataVisualization.Charting and System.Windows.Forms.DataVisualization.Charting)
- After selecting chart, use your mouse to click/drag a chart area onto the Form1 window. It may look like a bar chart but you'll be able to use it in other ways.
- Make a method you can call to plot your data.
- Double-click the "Form1" item within the Visual Studio solution explorer. Form1.cs should appear in the text editor window.
- Add the Plot method below to the class Form1.
public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void Plot(List<double> xs, List<double> ys, string name) { chart1.Series.Clear(); chart1.Series.Add(name); chart1.Series.Last().ChartType = SeriesChartType.Point; //SeriesChartType.Line for (int i = 0; i < xs.Count; i++) chart1.Series.Last().Points.AddXY(xs[i], ys[i]); chart1.Series.Last().Points.DataBindXY(xs, "xs", ys, "ys"); //chart1.Series.Last().YAxisType = AxisType.Secondary; //chart1.ChartAreas[0].AxisX.Minimum = 0.0; // xmin on plot //chart1.ChartAreas[0].AxisY.IsReversed = true; //flip y order //chart1.Size = new Size(900, 900); //resize plot } } - Change the project from an application to a library.
- Within the solution explorer, right click on the project and select "Properties"
- In the Application menu change the "Output type" to "Class Library"
- Build the solution and ensure there are no errors
- Use your new library.
- In the solution explorer, locate the project that you'd like to use this plot library from and right-click the "references" sub-item and left click "Add reference"
- In this reference manager add System.Windows.Forms under "Assemblies" and add your new library project under "Solution" and click OK
- Open a source file in that you'd like to use the plot library from and add the line "using System.Windows.Forms;" to the top and add a using statement for the namespace you just created in the plot library.
- Now just add the following three lines and run this application
Form1 f = new Form1(); f.Plot(new List<double>() {0,1,2}, new List<double>() {0,1,4}, "square"); Application.Run(f); - If that code is reached at runtime you should see a plot containing 3 data points with x=0,1,2 and y=x^2.

Comments
Post a Comment