Skip to content

Commit

Permalink
#18 feat: Add Axis example
Browse files Browse the repository at this point in the history
  • Loading branch information
NonoMalpi committed Feb 22, 2021
1 parent cfd1bf8 commit 59863da
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Examples/003-Axis/Axis.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Axis</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SharpPlot" Version="0.3.0" />
</ItemGroup>

</Project>
Binary file added Examples/003-Axis/Figures/axis-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Examples/003-Axis/Figures/axis-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Examples/003-Axis/Figures/axis-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions Examples/003-Axis/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SharpPlot;
using SharpPlot.Canvas.Figure;
using SharpPlot.Utils;

namespace Axis
{
public static 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, "sin(x)");

Gnuplot.Axis.SetYRange(-2, 2);
Gnuplot.Axis.SetXTicks(start:-4, step: 0.5, stop:4);

Gnuplot.Axis.SetXLabel(label: "time [s]");
Gnuplot.Axis.SetYLabel(label: "Amplitude [mm]", rotation: 90);

Gnuplot.Show();
Gnuplot.Wait();

Gnuplot.CleanData();

var (id2, fig2) = Gnuplot.Plot<Line>(data, "sin(x)");

var additionalXTicks = new Dictionary<string, double>() { {"pi", Math.PI}, {"e", Math.E} };
var additionalYTicks = new Dictionary<string, double>() { {"-pi", (-1)*Math.PI}, {"-e", (-1)*Math.E} };

Gnuplot.Axis.SetXRange(0, 8);
Gnuplot.Axis.AddTicks(labelValues: additionalXTicks, axis: 0);
Gnuplot.Axis.SetYTicks(ticks: new List<double>(){-4,-2, -1, -0.5, -0.25, 0, 0.25, 0.5, 1, 2, 4});
Gnuplot.Axis.AddTicks(labelValues: additionalYTicks, axis: 1);

Gnuplot.Show();
Gnuplot.Wait();

Gnuplot.CleanData();

var data3D = new DataPoints(x: x, y: sinX, z: sinX);
Gnuplot.SetPlotType(PlotType.Splot);
var (id3, fig3) = Gnuplot.Plot<Line>(data3D);
Gnuplot.Axis.RemoveXTicks();
Gnuplot.Axis.SetYTicks(start: -2, stop: 2, num: 11);
Gnuplot.Axis.SetZRange(-1, 1);
Gnuplot.Axis.SetZLabel(label: "z", rotation: 45);
var additionalZTicks = new Dictionary<string, double>() { {"min", -0.75}, {"max", 0.75} };
Gnuplot.Axis.AddTicks(additionalZTicks, axis: 2);

Gnuplot.Show();
Gnuplot.Wait();
}
}
}
113 changes: 113 additions & 0 deletions Examples/003-Axis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
## 003 - Axis

This example shows how to handle `Axis` object in order to customise default axes.

`Axis` is a static attribute initialised after every `Gnuplot.Start()` or `Gnuplot.CleanData()`. The behaviour and methods are the
same regardless the chosen direction, i.e. X, Y or Z.
Each `Axis` object has three main component:
* Range: It defines the axis span (the values to represent between a minimum and a maximum value).
* Ticks: The marks (values) to represent over the axis.
* Label: The axis name or meaning.

Each `Axis` trait can be modified through the set method of the `Axis` class, e.g. `Gnuplot.Axis.SetXRange()` or `Gnuplot.Axis.SetYTicks()`.

To run the code of axis examples:

1. Go to the folder `Examples/003-Axis`

```shell
(SharpPlot)$ cd Examples/003-Axis
```

2. The only thing you need to do now is running the project:

```shell
(003-Axis)$ dotnet run
Press any key + Enter to continue...
```

And you'll get the following set of figures:

<img src="Figures/axis-1.png" alt="figure-properties" style="zoom:66%;"/>

<img src="Figures/axis-2.png" alt="figure-properties" style="zoom:66%;"/>

<img src="Figures/axis-3.png" alt="figure-properties" style="zoom:66%;"/>

The code that has produced the figures is the following:

```c#
using System;
using System.Collections.Generic;
using System.Linq;
using SharpPlot;
using SharpPlot.Canvas.Figure;
using SharpPlot.Utils;

namespace Axis
{
public static 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, "sin(x)");

Gnuplot.Axis.SetYRange(-2, 2);
Gnuplot.Axis.SetXTicks(start:-4, step: 0.5, stop:4);

Gnuplot.Axis.SetXLabel(label: "time [s]");
Gnuplot.Axis.SetYLabel(label: "Amplitude [mm]", rotation: 90);

Gnuplot.Show();
Gnuplot.Wait();

Gnuplot.CleanData();

var (id2, fig2) = Gnuplot.Plot<Line>(data, "sin(x)");

var additionalXTicks = new Dictionary<string, double>() { {"pi", Math.PI}, {"e", Math.E} };
var additionalYTicks = new Dictionary<string, double>() { {"-pi", (-1)*Math.PI}, {"-e", (-1)*Math.E} };

Gnuplot.Axis.SetXRange(0, 8);
Gnuplot.Axis.AddTicks(labelValues: additionalXTicks, axis: 0);
Gnuplot.Axis.SetYTicks(ticks: new List<double>(){-4,-2, -1, -0.5, -0.25, 0, 0.25, 0.5, 1, 2, 4});
Gnuplot.Axis.AddTicks(labelValues: additionalYTicks, axis: 1);

Gnuplot.Show();
Gnuplot.Wait();

Gnuplot.CleanData();

var data3D = new DataPoints(x: x, y: sinX, z: sinX);
Gnuplot.SetPlotType(PlotType.Splot);
var (id3, fig3) = Gnuplot.Plot<Line>(data3D);
Gnuplot.Axis.RemoveXTicks();
Gnuplot.Axis.SetYTicks(start: -2, stop: 2, num: 11);
Gnuplot.Axis.SetZRange(-1, 1);
Gnuplot.Axis.SetZLabel(label: "z", rotation: 45);
var additionalZTicks = new Dictionary<string, double>() { {"min", -0.75}, {"max", 0.75} };
Gnuplot.Axis.AddTicks(additionalZTicks, axis: 2);

Gnuplot.Show();
Gnuplot.Wait();
}
}
}
```
There are several things to understand from this example:
* The axis span can be set with `Gnuplot.Axis.SetXRange()`, `Gnuplot.Axis.SetYRange()` or `Gnuplot.Axis.SetZRange()`, where
the minimum and maximum values are specified. By default, this method sets 5 ticks between both limits.
* In order to define axis span and ticks simultaneously, `Gnuplot.Axis.SetXTicks()`, `Gnuplot.Axis.SetYTicks()` or `Gnuplot.Axis.SetZTicks()` provide
better handling. The ticks can be initialised as a custom-defined `IEnumerable<double>` or a fixed-length sequence (start-step-stop or start-stop-number).
* Additional ticks can be included using `Gnuplot.Axis.AddTicks()` method, whereas the entire axis ticks can be removed using `Gnuplot.Axis.RemoveXTicks()`,
`Gnuplot.Axis.RemoveYTicks()` or `Gnuplot.Axis.RemoveZTicks()`.
* The axis label is added using `Gnuplot.Axis.SetXLabel()`, `Gnuplot.Axis.SetYLabel()` or `Gnuplot.Axis.SetZLabel()`. The rotation parameter configures
the label orientation.
* A 3D figure can be plotted setting the plot type to `Splot` in `Gnuplot.SetPlotType()` method.

14 changes: 14 additions & 0 deletions Examples/Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GettingStarted", "001-Getti
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FigureProperties", "002-FigureProperties\FigureProperties.csproj", "{73BDB2AC-892C-4986-9779-D890236F98B9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Axis", "003-Axis\Axis.csproj", "{947DFB64-72D8-41E8-97FF-BBD462B5979F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -44,5 +46,17 @@ Global
{73BDB2AC-892C-4986-9779-D890236F98B9}.Release|x64.Build.0 = Release|Any CPU
{73BDB2AC-892C-4986-9779-D890236F98B9}.Release|x86.ActiveCfg = Release|Any CPU
{73BDB2AC-892C-4986-9779-D890236F98B9}.Release|x86.Build.0 = Release|Any CPU
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Debug|x64.ActiveCfg = Debug|Any CPU
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Debug|x64.Build.0 = Debug|Any CPU
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Debug|x86.ActiveCfg = Debug|Any CPU
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Debug|x86.Build.0 = Debug|Any CPU
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Release|Any CPU.Build.0 = Release|Any CPU
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Release|x64.ActiveCfg = Release|Any CPU
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Release|x64.Build.0 = Release|Any CPU
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Release|x86.ActiveCfg = Release|Any CPU
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 59863da

Please sign in to comment.