Skip to content

Commit 59863da

Browse files
committed
#18 feat: Add Axis example
1 parent cfd1bf8 commit 59863da

File tree

7 files changed

+202
-0
lines changed

7 files changed

+202
-0
lines changed

Examples/003-Axis/Axis.csproj

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<RootNamespace>Axis</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="SharpPlot" Version="0.3.0" />
11+
</ItemGroup>
12+
13+
</Project>

Examples/003-Axis/Figures/axis-1.png

15.3 KB
Loading

Examples/003-Axis/Figures/axis-2.png

13.2 KB
Loading

Examples/003-Axis/Figures/axis-3.png

24.7 KB
Loading

Examples/003-Axis/Program.cs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using SharpPlot;
5+
using SharpPlot.Canvas.Figure;
6+
using SharpPlot.Utils;
7+
8+
namespace Axis
9+
{
10+
public static class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
Gnuplot.Start();
15+
16+
var x = Enumerable.Range(-100, 201).Select(z=>z*0.025*Math.PI).ToArray();
17+
var sinX = x.Select(Math.Sin).ToArray();
18+
var data = new DataPoints(x, sinX);
19+
20+
var (id, fig) = Gnuplot.Plot<Line>(data, "sin(x)");
21+
22+
Gnuplot.Axis.SetYRange(-2, 2);
23+
Gnuplot.Axis.SetXTicks(start:-4, step: 0.5, stop:4);
24+
25+
Gnuplot.Axis.SetXLabel(label: "time [s]");
26+
Gnuplot.Axis.SetYLabel(label: "Amplitude [mm]", rotation: 90);
27+
28+
Gnuplot.Show();
29+
Gnuplot.Wait();
30+
31+
Gnuplot.CleanData();
32+
33+
var (id2, fig2) = Gnuplot.Plot<Line>(data, "sin(x)");
34+
35+
var additionalXTicks = new Dictionary<string, double>() { {"pi", Math.PI}, {"e", Math.E} };
36+
var additionalYTicks = new Dictionary<string, double>() { {"-pi", (-1)*Math.PI}, {"-e", (-1)*Math.E} };
37+
38+
Gnuplot.Axis.SetXRange(0, 8);
39+
Gnuplot.Axis.AddTicks(labelValues: additionalXTicks, axis: 0);
40+
Gnuplot.Axis.SetYTicks(ticks: new List<double>(){-4,-2, -1, -0.5, -0.25, 0, 0.25, 0.5, 1, 2, 4});
41+
Gnuplot.Axis.AddTicks(labelValues: additionalYTicks, axis: 1);
42+
43+
Gnuplot.Show();
44+
Gnuplot.Wait();
45+
46+
Gnuplot.CleanData();
47+
48+
var data3D = new DataPoints(x: x, y: sinX, z: sinX);
49+
Gnuplot.SetPlotType(PlotType.Splot);
50+
var (id3, fig3) = Gnuplot.Plot<Line>(data3D);
51+
Gnuplot.Axis.RemoveXTicks();
52+
Gnuplot.Axis.SetYTicks(start: -2, stop: 2, num: 11);
53+
Gnuplot.Axis.SetZRange(-1, 1);
54+
Gnuplot.Axis.SetZLabel(label: "z", rotation: 45);
55+
var additionalZTicks = new Dictionary<string, double>() { {"min", -0.75}, {"max", 0.75} };
56+
Gnuplot.Axis.AddTicks(additionalZTicks, axis: 2);
57+
58+
Gnuplot.Show();
59+
Gnuplot.Wait();
60+
}
61+
}
62+
}

Examples/003-Axis/README.md

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
## 003 - Axis
2+
3+
This example shows how to handle `Axis` object in order to customise default axes.
4+
5+
`Axis` is a static attribute initialised after every `Gnuplot.Start()` or `Gnuplot.CleanData()`. The behaviour and methods are the
6+
same regardless the chosen direction, i.e. X, Y or Z.
7+
Each `Axis` object has three main component:
8+
* Range: It defines the axis span (the values to represent between a minimum and a maximum value).
9+
* Ticks: The marks (values) to represent over the axis.
10+
* Label: The axis name or meaning.
11+
12+
Each `Axis` trait can be modified through the set method of the `Axis` class, e.g. `Gnuplot.Axis.SetXRange()` or `Gnuplot.Axis.SetYTicks()`.
13+
14+
To run the code of axis examples:
15+
16+
1. Go to the folder `Examples/003-Axis`
17+
18+
```shell
19+
(SharpPlot)$ cd Examples/003-Axis
20+
```
21+
22+
2. The only thing you need to do now is running the project:
23+
24+
```shell
25+
(003-Axis)$ dotnet run
26+
Press any key + Enter to continue...
27+
```
28+
29+
And you'll get the following set of figures:
30+
31+
<img src="Figures/axis-1.png" alt="figure-properties" style="zoom:66%;"/>
32+
33+
<img src="Figures/axis-2.png" alt="figure-properties" style="zoom:66%;"/>
34+
35+
<img src="Figures/axis-3.png" alt="figure-properties" style="zoom:66%;"/>
36+
37+
The code that has produced the figures is the following:
38+
39+
```c#
40+
using System;
41+
using System.Collections.Generic;
42+
using System.Linq;
43+
using SharpPlot;
44+
using SharpPlot.Canvas.Figure;
45+
using SharpPlot.Utils;
46+
47+
namespace Axis
48+
{
49+
public static class Program
50+
{
51+
static void Main(string[] args)
52+
{
53+
Gnuplot.Start();
54+
55+
var x = Enumerable.Range(-100, 201).Select(z=>z*0.025*Math.PI).ToArray();
56+
var sinX = x.Select(Math.Sin).ToArray();
57+
var data = new DataPoints(x, sinX);
58+
59+
var (id, fig) = Gnuplot.Plot<Line>(data, "sin(x)");
60+
61+
Gnuplot.Axis.SetYRange(-2, 2);
62+
Gnuplot.Axis.SetXTicks(start:-4, step: 0.5, stop:4);
63+
64+
Gnuplot.Axis.SetXLabel(label: "time [s]");
65+
Gnuplot.Axis.SetYLabel(label: "Amplitude [mm]", rotation: 90);
66+
67+
Gnuplot.Show();
68+
Gnuplot.Wait();
69+
70+
Gnuplot.CleanData();
71+
72+
var (id2, fig2) = Gnuplot.Plot<Line>(data, "sin(x)");
73+
74+
var additionalXTicks = new Dictionary<string, double>() { {"pi", Math.PI}, {"e", Math.E} };
75+
var additionalYTicks = new Dictionary<string, double>() { {"-pi", (-1)*Math.PI}, {"-e", (-1)*Math.E} };
76+
77+
Gnuplot.Axis.SetXRange(0, 8);
78+
Gnuplot.Axis.AddTicks(labelValues: additionalXTicks, axis: 0);
79+
Gnuplot.Axis.SetYTicks(ticks: new List<double>(){-4,-2, -1, -0.5, -0.25, 0, 0.25, 0.5, 1, 2, 4});
80+
Gnuplot.Axis.AddTicks(labelValues: additionalYTicks, axis: 1);
81+
82+
Gnuplot.Show();
83+
Gnuplot.Wait();
84+
85+
Gnuplot.CleanData();
86+
87+
var data3D = new DataPoints(x: x, y: sinX, z: sinX);
88+
Gnuplot.SetPlotType(PlotType.Splot);
89+
var (id3, fig3) = Gnuplot.Plot<Line>(data3D);
90+
Gnuplot.Axis.RemoveXTicks();
91+
Gnuplot.Axis.SetYTicks(start: -2, stop: 2, num: 11);
92+
Gnuplot.Axis.SetZRange(-1, 1);
93+
Gnuplot.Axis.SetZLabel(label: "z", rotation: 45);
94+
var additionalZTicks = new Dictionary<string, double>() { {"min", -0.75}, {"max", 0.75} };
95+
Gnuplot.Axis.AddTicks(additionalZTicks, axis: 2);
96+
97+
Gnuplot.Show();
98+
Gnuplot.Wait();
99+
}
100+
}
101+
}
102+
```
103+
There are several things to understand from this example:
104+
* The axis span can be set with `Gnuplot.Axis.SetXRange()`, `Gnuplot.Axis.SetYRange()` or `Gnuplot.Axis.SetZRange()`, where
105+
the minimum and maximum values are specified. By default, this method sets 5 ticks between both limits.
106+
* In order to define axis span and ticks simultaneously, `Gnuplot.Axis.SetXTicks()`, `Gnuplot.Axis.SetYTicks()` or `Gnuplot.Axis.SetZTicks()` provide
107+
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).
108+
* Additional ticks can be included using `Gnuplot.Axis.AddTicks()` method, whereas the entire axis ticks can be removed using `Gnuplot.Axis.RemoveXTicks()`,
109+
`Gnuplot.Axis.RemoveYTicks()` or `Gnuplot.Axis.RemoveZTicks()`.
110+
* The axis label is added using `Gnuplot.Axis.SetXLabel()`, `Gnuplot.Axis.SetYLabel()` or `Gnuplot.Axis.SetZLabel()`. The rotation parameter configures
111+
the label orientation.
112+
* A 3D figure can be plotted setting the plot type to `Splot` in `Gnuplot.SetPlotType()` method.
113+

Examples/Examples.sln

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GettingStarted", "001-Getti
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FigureProperties", "002-FigureProperties\FigureProperties.csproj", "{73BDB2AC-892C-4986-9779-D890236F98B9}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Axis", "003-Axis\Axis.csproj", "{947DFB64-72D8-41E8-97FF-BBD462B5979F}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -44,5 +46,17 @@ Global
4446
{73BDB2AC-892C-4986-9779-D890236F98B9}.Release|x64.Build.0 = Release|Any CPU
4547
{73BDB2AC-892C-4986-9779-D890236F98B9}.Release|x86.ActiveCfg = Release|Any CPU
4648
{73BDB2AC-892C-4986-9779-D890236F98B9}.Release|x86.Build.0 = Release|Any CPU
49+
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
50+
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Debug|Any CPU.Build.0 = Debug|Any CPU
51+
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Debug|x64.ActiveCfg = Debug|Any CPU
52+
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Debug|x64.Build.0 = Debug|Any CPU
53+
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Debug|x86.ActiveCfg = Debug|Any CPU
54+
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Debug|x86.Build.0 = Debug|Any CPU
55+
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Release|Any CPU.ActiveCfg = Release|Any CPU
56+
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Release|Any CPU.Build.0 = Release|Any CPU
57+
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Release|x64.ActiveCfg = Release|Any CPU
58+
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Release|x64.Build.0 = Release|Any CPU
59+
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Release|x86.ActiveCfg = Release|Any CPU
60+
{947DFB64-72D8-41E8-97FF-BBD462B5979F}.Release|x86.Build.0 = Release|Any CPU
4761
EndGlobalSection
4862
EndGlobal

0 commit comments

Comments
 (0)