Skip to article frontmatterSkip to article content

Troubleshooting

Communicating with Gnuplot


While tinkering with a plot, there are times that you get an unexpected output or anything at all. G3P provides various ways to communicate with gnuplot instance to get more information and those are the subjects of the next sections.

Auto-assertion in Jupyter Notebook

When you display a plot in a Jupyter Notebook, if there was an error reported by Gnuplot, G3P shows it as the output of the cell. Let’s demonstrate this with an example.

One common mistake when using G3P’s C++ convention is to forget the newline character at the end of a command. If that happens in a notebook’s cell, G3P automatically asserts with an error message:

#include <g3p/gnuplot>

g3p::gnuplot gp;
gp << "set key left box"
   << "set samples 200\n"
   << "plot [-30:20] besj0(x)*0.12e1 with impulses, (x**besj0(x))-2.5 with points\n"
Output
Loading...

gnuplot::log() function

The above error doesn’t tell you anything about where that happened. To find out what went wrong, you have to run gnuplot::log() function:

gp.log()
Output
">> gnuplot log generated by g3p << version: 6.0 patchlevel: 2 system: Linux arch: x86_64 gnuplot> set key left box set samples 200 ^ line 0: unknown key option line 0: warning: Did you try to plot a complex-valued function? 1 neLdrZKj pngcairo LjtCKqJQ unknown key option MDfHvXtA "

As you can see in the output there’s a missing newline character between box and set. There are also some gibberish texts at end of the output that you can safely ignore as they’re necessary for G3P normal operation.

Getting Gnuplot version information

Depending on the version of Gnuplot, the same sequence of commands may generate different plots. You can use g3p::gnuplot’s family of version() functions to switch to the right sequence of commands accordingly (for example, see version-based switching of terminals):

gp.version_string()
Output
"6.0.2"
gp.version()
Output
6.0000000
gp.patchlevel()
Output
"2"

Communicating with Gnuplot

Gnuplot has a long list of show commands that report various settings and also a print command that can show the value of an expression. Using g3p::gnuplot overloaded >> , you can extract the information you need.

For example, here’s how you can find the current terminal:

std::string term;
gp("print GPVAL_TERM") >> 1 >> term;
term
Output
"pngcairo"

The immediate number after the first >> is the number of lines to read from the end of the output.

For our next example, we use a multi-line output. We’ll extract the rotation around x axis as a float value. To show the current view’s settings, we have to use show view command in Gnuplot. Here’s the verbatim output of show view command:

	view is 60 rot_x, 30 rot_z, 1 scale, 1 scale_z
		 axes are independently scaled
		 azimuth 0
 

So, we have to extract 4 lines. Then, starting from the first line, skip twice to get our value:

std::string skip;
float x_rot;
gp("show view") >> 4 >> skip >> skip >> x_rot;
x_rot
Output
60.0000f