|
| 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 | + |
0 commit comments