Skip to content

How to make Plots in C# Programming Language, and an step by step guide plus a simulation video tutorial

Notifications You must be signed in to change notification settings

mohammadijoo/C_Sharp_Plot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

C# Plotting Playground

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

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 plot
  • CSharpPlot.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.


Library: ScottPlot

What is ScottPlot?

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.

Installation

ScottPlot is added via NuGet to each Console App project:

dotnet add package ScottPlot

In Visual Studio, this is done using the NuGet Package Manager UI (steps shown below).


Prerequisites

  • 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)

Setup and run (Visual Studio 2022)

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.

Step-by-step: Create a Blank Solution + Two Console Apps

  1. Create the solution
    • Open Visual Studio 2022
    • FileNewProject
    • Choose Blank Solution
    • Name it something like CSharpPlot
  2. Add the line plotting project
    • Right-click the Solution → AddNew Project...
    • Select Console App (.NET)
    • Name the project: CSharpPlot.Line
    • Install ScottPlot:
      • Right-click CSharpPlot.LineManage NuGet Packages...
      • Browse → search ScottPlotInstall
    • Replace the content of Program.cs with the code from your line module file
  3. Add the histogram plotting project
    • Right-click the Solution → AddNew Project...
    • Select Console App (.NET)
    • Name the project: CSharpPlot.Histogram
    • Install ScottPlot (same steps as above)
    • Replace the content of Program.cs with the code from your histogram module file
  4. Run a specific module
    • Right-click the project you want to run (Line or Histogram) → Set as Startup Project
    • Run: DebugStart 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.


Setup and run (.NET CLI)

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 run

For this repository, the Visual Studio solution structure is recommended because it keeps each module runnable without swapping files.


Project structure

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.


Output files and working directory

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.


Line plots module

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.

Key namespaces / imports

  • ScottPlot — plotting API
  • System, System.Linq — numeric generation and transformations
  • System.IO — creating output directories and local file loading
  • System.Net.Http — downloading CSV files from a URL (optional)
  • System.Globalization — locale-safe numeric parsing for CSV values

What the module does

  • 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 like Linspace() and applies different styles.
  • Subplots using Multiplot
    Uses ScottPlot.Multiplot with 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).

CSV workflow

  • 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");

Extending this module

  • 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.)

Histogram plots module

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.

Key namespaces / imports

  • ScottPlot and ScottPlot.Statistics — histogram bins and rendering
  • System, System.Linq — data transformation and numeric operations
  • System.IO — output directory and optional local CSV loading
  • System.Threading — optional delays for iterative rendering demonstrations
  • System.Net.Http, System.Globalization — URL-based CSV downloads and safe parsing

What the module does

  • 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.

Random data generation

The module includes a standard normal generator (commonly implemented using the Box–Muller transform) to create reproducible distributions for histogram demonstrations.

Extending this module

  • Add additional normalization modes (count, probability, density)
  • Introduce stacked histograms or grouped comparisons
  • Expand CSV examples (multiple columns, filters, subsets, stratified distributions)

Implementation tutorial video

A complete walkthrough is available on YouTube (click the image below).

C# Plotting with ScottPlot - Implementation Tutorial

About

How to make Plots in C# Programming Language, and an step by step guide plus a simulation video tutorial

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages