At first glance, it might seem that Gnuplot is more difficult to use for plotting than MS Excel. But it only seems that way, the entry threshold is a little higher (you can’t use a mouse to click it, you need to read the documentation here), but in practice, it comes out much easier and more convenient. I wrote a script once and have been using it all my life. It’s really much more difficult for me to plot a graph in Excel, where everything is not logical than in Gnuplot. And the main advantage of Gnuplot is that you can embed it into your programs and visualize data on the fly. Also Gnuplot without any problems builds a graph from a 30-gigabyte file of statistical data, while Exel simply crashed and could not open it.
The pluses of Gnuplot include the fact that it can be easily integrated into the code in standard programming languages. There are ready-made libraries for many languages, I personally came across Fortran and Python. Thus, you can generate graphs directly from your program.
Gnuplot — real application
There are two ways to work with Gnuplot: command mode and script execution mode. I recommend using the second mode immediately, as it is the most convenient and fastest. Moreover, the functionality is absolutely the same.
The graph is plotted by the
plot command. As command parameters, you can specify a function or the name of the data file. As well as which column of data to use and how to connect the points, how to denote them, etc. Let’s illustrate.
gnuplot> plot sin(x)
After executing the command, a window will open where there will be a sine graph with default signatures. Let’s improve this graph and figure out some additional options at the same time.
Let’s sign the axes.
set xlabel "X"
Specifies the label for the abscissa axis.
set ylabel "Y"
Specifies the label for the ordinate axis.
Let’s add a grid to show where the graph is built.
We will set the limits of the values to which the graph will be limited via the command
set yrange [-1.1:1.1]
Thus, the graph will be drawn from the minimum value of -1.1 to a maximum value of 1.1.
Likewise, I set the range for the abscissa so that only one period of the sinusoid is visible.
We need to add a title to our schedule:
set title "Gnuplot's examples" font "Helvetica Bold, 20"
Note that you can set fonts and their sizes. See the Gnuplot documentation for what fonts can be used.
And finally, in addition to the sine on the graph, let’s also draw the cosine, and also set the line type and its color. And also add legends, what are we drawing.
plot sin(x) title "sin(x)" lc rgb "red", cos(x) title "cos(x)" lc rgb "green"
Here we draw two graphs on one canvas, in red and green. In general, there are a lot of options for lines (dotted line, stroke, solid), line widths, colors. Like the types of points. My goal is only to demonstrate the range of possibilities. Let’s bring all the commands into one pile and execute them sequentially.
If you honestly repeated all this after me, you might have noticed that manually entering it every time, even copying, is somehow not comme il faut. But is this a ready-made script? So let’s do it!
We make it executable and run it.
chmod +x gnuplottest.gpi
As a result, we get the same window with charts. If you don’t add
-persist to the title, the window will automatically close after the script is executed.
In order to output data to a file and not to the screen, you need to reassign the terminal.
set terminal png size 800, 600 set output "result.png" rep
As you might guess, we indicate the file type, its resolution, then we indicate the file name. Add these two lines to the beginning of our script, and we get this picture in the current folder.
In order to save to postscript, you need to use the following commands:
set terminal postscript eps enhanced color solid set output "result.ps" rep
Here a simple code for plot a cosine function in Python using
import numpy as np import matplotlib.pyplot as plt t = np.arange(0.0,2.0,0.01) s = 1+ np.cos(2*np.pi*t) plt.plot(t,s,'*') plt.xlabel('Time(t)') plt.ylabel('Voltage(mV)') plt.title('Cosine wave plot(cos(x))') plt.show()