A minimal, extensible C# project showcasing scientific plotting with ScottPlot, starting with line plots and histograms. The repository is structured to make it easy to add additional plot types (e.g., pie charts, bar charts, scatter plots, heatmaps) over time.
Built with .NET (Console Apps), NuGet, and Visual Studio 2022.
- About this repository
- Library: ScottPlot
- Prerequisites
- Setup and run (Visual Studio 2022)
- Setup and run (.NET CLI)
- Project structure
- Output files and working directory
- Line plots module
- Histogram plots module
- Implementation tutorial video
This repository is a focused playground for plotting in C# using ScottPlot. Each plot category is implemented as a standalone, runnable Console App project to keep examples independent and easy to expand.
Current examples:
CSharpPlot.Line— line plots, multi-series overlays, subplots (multiplot layouts), and a CSV-based plotCSharpPlot.Histogram— histogram binning/normalization patterns, overlays, PDF comparison, and a CSV-based histogram
New plot categories can be added later by creating additional Console App projects
(e.g., CSharpPlot.Pie, CSharpPlot.Scatter) and adding modular
documentation sections similar to the ones below.
ScottPlot is a free and open-source plotting library for .NET. It supports generating publication-quality static images and interactive plots across common .NET application types. This repository uses ScottPlot to create plots and export them as PNG images from console apps.
ScottPlot is added via NuGet to each Console App project:
dotnet add package ScottPlotIn Visual Studio, this is done using the NuGet Package Manager UI (steps shown below).
- Visual Studio 2022 with the Desktop development with .NET workload
- .NET SDK (recommended: .NET 6+)
- Internet access for CSV URL examples (optional; local CSV loading is supported too)
This repository follows a clean Visual Studio workflow: a single solution with multiple independent
Console App projects. Each project has its own Program.cs entry point and runs independently.
-
Create the solution
- Open Visual Studio 2022
- File → New → Project
- Choose Blank Solution
- Name it something like
CSharpPlot
-
Add the line plotting project
- Right-click the Solution → Add → New Project...
- Select Console App (.NET)
- Name the project:
CSharpPlot.Line - Install ScottPlot:
- Right-click
CSharpPlot.Line→ Manage NuGet Packages... - Browse → search
ScottPlot→ Install
- Right-click
- Replace the content of
Program.cswith the code from yourlinemodule file
-
Add the histogram plotting project
- Right-click the Solution → Add → New Project...
- Select Console App (.NET)
- Name the project:
CSharpPlot.Histogram - Install ScottPlot (same steps as above)
- Replace the content of
Program.cswith the code from yourhistogrammodule file
-
Run a specific module
- Right-click the project you want to run (Line or Histogram) → Set as Startup Project
- Run: Debug → Start Without Debugging (Ctrl+F5) or press F5
Tip: Each project produces images under its own output folder. See the Output files and working directory section for details.
If you prefer not to use Visual Studio, you can build and run the projects using the .NET CLI. This repository supports a simple console-app workflow:
dotnet new console -n CSharpPlot
cd CSharpPlot
dotnet add package ScottPlot
# replace Program.cs with the desired module file (line or histogram), then:
dotnet runFor this repository, the Visual Studio solution structure is recommended because it keeps each module runnable without swapping files.
A typical structure for this repository looks like this:
CSharpPlot.sln
CSharpPlot.Line/
Program.cs
CSharpPlot.Line.csproj
CSharpPlot.Histogram/
Program.cs
CSharpPlot.Histogram.csproj
When you add new plot categories later, create new Console App projects:
CSharpPlot.Pie, CSharpPlot.Bar, CSharpPlot.Scatter, etc.
The examples save plots as image files (PNG) into an output directory.
In Visual Studio, the process working directory can differ from the project folder, so
output files may appear under the build directory (e.g., bin\Debug\net8.0\output).
Recommended pattern:
For deterministic output placement (next to the executable), set the output folder using:
string outDir = Path.Combine(AppContext.BaseDirectory, "output");
Directory.CreateDirectory(outDir);This ensures images always go to the same place regardless of how the program is launched.
The line plotting module demonstrates core plotting patterns using ScottPlot: multi-series overlays, line styling, markers, subplots (multiplot), and producing static PNG outputs. It also includes a real-world example that downloads a CSV dataset and generates a plot from it.
ScottPlot— plotting APISystem,System.Linq— numeric generation and transformationsSystem.IO— creating output directories and local file loadingSystem.Net.Http— downloading CSV files from a URL (optional)System.Globalization— locale-safe numeric parsing for CSV values
-
Multiple line series on one plot
Adds several line series to the same axes (overlays) and customizes line patterns and markers. -
Plotting multiple sequences
Plots multiple vectors (arrays/lists) as independent series with optional legend entries. -
Trigonometric function plots
Generates smooth curves using a helper likeLinspace()and applies different styles. -
Subplots using Multiplot
UsesScottPlot.Multiplotwith a grid layout to render multiple subplots into one image. -
CSV-based real-world plot
Downloads a CSV dataset and plots a derived relationship (e.g., aggregation such as mean vs. binned x-values).
- Download CSV text using
HttpClient(URL-based datasets) - Parse header + rows with a lightweight CSV splitter (quote-aware)
- Select columns by name (e.g.,
carat,price) - Transform/aggregate values (e.g., binning x-values, averaging y-values)
- Plot the result and export a PNG
Local CSV loading:
If you want to read CSV from your local drive instead of a URL, use:
// string csvText = File.ReadAllText(@"C:\path\to\yourfile.csv");- Add additional line-series examples by appending new blocks
- Introduce annotations (titles, labels, legends) consistently
- Add new CSV datasets and plot strategies (scatter, box plots, rolling averages, etc.)
The histogram module focuses on distribution visualization using ScottPlot. It covers: bin selection strategies, normalization variants, overlays for comparison, and an example that reads a CSV dataset.
ScottPlotandScottPlot.Statistics— histogram bins and renderingSystem,System.Linq— data transformation and numeric operationsSystem.IO— output directory and optional local CSV loadingSystem.Threading— optional delays for iterative rendering demonstrationsSystem.Net.Http,System.Globalization— URL-based CSV downloads and safe parsing
-
Basic histograms
Creates histograms from generated data and exports the result as PNG. -
Bin count strategies
Demonstrates how bin counts can be chosen using different rules (e.g., square-root, Sturges, Scott, Freedman–Diaconis). -
Iterative bin updates
Shows how changing bin count affects the histogram (useful when tuning visualization). -
Custom bin edges
Builds histograms from explicit bin edges and renders bars with bin-width-aware sizing. -
Categorical bar histogram
Counts occurrences of string categories and renders them as a bar chart with labeled ticks. -
Overlay comparisons
Overlays two distributions using consistent binning so shapes can be compared directly. -
PDF normalization + theoretical curve
Normalizes histogram bars to represent a probability density estimate and overlays a theoretical PDF curve. -
CSV-based real-world histogram
Reads a numeric column from a CSV dataset (e.g., price) and plots its distribution.
The module includes a standard normal generator (commonly implemented using the Box–Muller transform) to create reproducible distributions for histogram demonstrations.
- Add additional normalization modes (count, probability, density)
- Introduce stacked histograms or grouped comparisons
- Expand CSV examples (multiple columns, filters, subsets, stratified distributions)
A complete walkthrough is available on YouTube (click the image below).