This first example is aimed at every beginner to start playing with the library, and it also serves as a template first project to see whether everything has been set-up properly.
To run the example:
-
Go to the folder
Examples/001-GettingStarted
(SharpPlot)$ cd Examples/001-GettingStarted
-
The only thing you need to do now is running the project:
(001-GettingStarted)$ dotnet run Press any key + Enter to continue...
And you'll get:
The code that has produced such a nice figure is the following:
using System;
using System.Linq;
using SharpPlot;
using SharpPlot.Canvas.Figure;
namespace GettingStarted
{
class Program
{
static void Main(string[] args)
{
Gnuplot.Start();
var x = Enumerable.Range(-100, 201).Select(z=>z*0.025*Math.PI).ToArray();
var sinX = x.Select(Math.Sin).ToArray();
var data = new DataPoints(x, sinX);
var (id, fig) = Gnuplot.Plot<Line>(data, "First Example: sin(x)");
Gnuplot.Show();
Gnuplot.Wait();
}
}
}
There are several things to understand from this example:
- We need to import the library with:
using SharpPlot
- The initialisation of the
gnuplot
interpreter happens withGnuplot.Start()
. This is the default constructor ofSharpPlot.Gnuplot
. - In case
SharpPlot.Gnuplot
is not able to initialise or gives any error, try theexplicit
constructorGnuplot("{gnuplot/Bin/Path}")
, which allows you to specify where the executable of gnuplot is located at in your computer. DataPoints
is a collection of data, which must be commensurate, i.e. the length ofx
must be equal to the length ofsinX
- To represent the data with a line, we use
Plot<Line>
, which accepts the legend of thisDataPoints
as second argument.
And this is all for the time being. Congratulations, and see you in the next Example 👏