Gnuplot: is there a way to add small gif icon on the plot? - gnuplot

I am looking for a way to add small icons on the plot, like OK or KO symbols, flags, arrows and such.
So far I've seen an example where you can add a background image, but nothing that actually allow me to set a point on the plot, like I could do with a label, and apply there an icon.
Is there a way to do so?

Call me obsessive but as usual this can be done with the epslatex terminal, embedding the image using a set label statement as you would do in regular latex. If your image is so-icon.png, then within gnuplot do:
set terminal epslatex standalone header "\\usepackage{graphicx}"
set output "plot.tex"
set label at screen 0.5,0.5 '\includegraphics{so-icon.png}'
plot sin(x)
And now run pdflatex
pdflatex plot.tex
Your output will be named plot.pdf and look like this:
Change the positioning of the label (help set label for more info) to wherever you want. You can also use the formatting options of \includegraphics{}, for example \includegraphics[width=2cm]{} for a 2cm wide version of your image (sorry, I don't do inches!).
Note that if you want to embed PNG, JPEG, GIF, PDF and so on, the pdflatex command is required, you cannot use regular latex for those.

Related

Introduce a box with special symbols in Gnuplot

I want to have a gnuplot with a box on the right top corner like the one below. My difficulties are both:
how to get \hat{Q} in a the legend?
how to add a box containing the titles?
set term cairolatex eps standalone size 3in,3in
set output 'Q.tex'
set key box opaque samplen 0.5
plot for [i=1:4] sinc(x)**i title sprintf('\tiny${\hat{Q}=%d}$',i) lw 2
The standalone keyword wraps the output in minimal LaTeX commands so that you can run latex Q directly. Omit this if you want to produce a LaTeX fragment for including in a larger document.
I show how to construct a formatted title that includes a LaTeX font size. Note that the format uses single quotes rather than double quotes.
This example uses cairolatex eps because you specifically asked about an eps file. Normallay I would use cairolatex png or cairolatex pdf and process afterwards with pdflatex rather than plain latex.
If you want to include your figure into LaTeX, you should use the terminal cairolatex, then you can include LaTeX code in your plots:
plot "your.data" title '$\hat{Q}=10$'
Concerning your second question, have a look at the manual concerning set key.

Not the same font in gnuplot when using command line or gnuplot commands

I have a file containing some commands :
$ cat components.plg
set terminal png
set output output
set font "Helvetica,10"
unset xtics
unset ytics
unset border
plot "reportGraph_0" with lines title "Component 0", "reportGraph_1" with lines title "Component 1", "aoi.txt" with lines title "Area of interest"
$
I won't display the contents of reportGraph_# here, but anything with 2 points in them is valid.
I then launch gnuplot with this file as parameter, and I get a warning about Arial (I'm not entirely sure why).
$ gnuplot -e "output='footprint.png' " components.plg
Could not find/open font when opening font "arial", using internal non-scalable font
$
However, the result image has its text in non-Helvetica (but more in something like Monospace sans).
Here are the two images (first the output of the command line, second the output of the same sequence of commands launched in a gnuplot shell, and exporting the result image). There is one difference, I didn't use the first two commands (set terminal png and set output output) in gnuplot - I merely "exported" the image as png.
The question is : why don't I have the same font in these two images?
I would like to have Helvetica (or Lucida Grande, but not a monospace font).
Gnuplot has a large variety of terminals. When you launch an interactive gnuplot session and plot you probably use the wxt or qt terminal. By exporting the image from the plot window you save the image as theyes were created by those terminals.
Now, with set terminal png you use a different and quite old terminal, with different capabilities, especially regarding font rendering.
If you want to save your image directly as png, use the pngcairoterminal, which produces higher quality images (the result should be equivalent to that of the exported image from wxt).
The pngcairo, or the pdfcairo terminals should also handle your fonts properly.
The png terminal is older than pngcairo, but it can still use a specified font. You can just specify a path to a .ttf or .pfa font file:
set terminal png font "/path/to/your/Helvetica.pfa"
or a comma-separated pair of the font file path and point size:
set terminal png font '/usr/share/fonts/liberation/LiberationSans-Regular.ttf,9'
gnuplot uses libgd so if you set the environment variable GDFONTPATH to the directory containing your .ttf or .pfa font files, you can specify just the font name instead of the full path:
set terminal png font 'LiberationSans-Regular'
Documentation for setting fonts with different terminals is available within gnuplot under help fonts gd, help fonts cairo, and help fonts postscript.

Consistent figure sizes with gnuplot

I am trying to make figures with gnuplot that will be included in a latex document. I want all figures to have the same dimensions and font size. To achieve it I found that I should specify the size of the figure in advance, which I do as such:
set terminal postscript eps size 3.4004,2.104 enhanced color
However, the resulting .eps figures have a lot of whitespace around them so I use the fixbb script to remove all unnecessary space. This changes the final size of the figure as well, which wouldn't be a problem as long as it is consistent. However, the amount of whitespace seems to vary from figure to figure, so my final figures have all different sizes.
This seems especially a problem with 3D plots.
Is there a way to make the size consistent while removing all unnecessary white space? In matplotlib there is the plt.tight_layout() command which does exactly that.
If by fixing the size of the figure you refer to the rectangle within which the data is plotted, you can set the margins manually. What I usually do for figures I put on my papers is use the epslatex terminal with the desired size and font, then set the margins, and then compile to pdf followed by a cropping to remove the white space. Example
set term epslatex color size 3.5,2.5 font 6
set output "gnuplot.eps"
set lmargin at screen 0.2
set rmargin at screen 0.98
set tmargin at screen 0.98
set bmargin at screen 0.1
plot sin(x) title '$\sin (x)$'
I embed this into a .tex file (which I call plot.tex):
\documentclass{article}
\usepackage[papersize={100cm,100cm}]{geometry}
\usepackage{graphicx}
\usepackage{color}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage[mathcal]{euscript}
\begin{document}
\thispagestyle{empty}
\include{gnuplot}
\end{document}
And run the following:
pdflatex plot.tex
pdfcrop plot.pdf
You will have a plot-crop.pdf with no white space around. Using different modifications of the above you can very precisely modify the size of your graph.
Finally, note that the epslatex terminal also allows the standalone option to avoid needing an external latex document to wrap your graph.

output.png from gnuplot is not as good as the figure from prompt shell

I often plot graphs in gnuplot prompt shell, like this:
gunuplot> plot sin(x) with linespoints pointtype 3
and the figure showed up is great.
Today, I save the graph in a .png file, like this:
gnuplot> set term png
gnuplot> set output "output.png"
gunuplot> plot sin(x) with linespoints pointtype 3
Then, I open output.png with eog in Ubuntu, like this:
$ eog output.png
I found that, the output.png displayed in eog is not as good as the figure plotted in prompt shell.
Why is that? Do I need to adjust some settings before save the output.png?
PS
I found that a way around it, first,
set term postscript
set output "output.ps"
then in linux shell,
$ convert output.ps output.jpg
This way some sort of solve the my problem.
The source of your problems with the PNG quality is most likely the missing antialiasing in the png terminal of Gnuplot. Since you give no screenshots, I'm not sure what you mean when talking about bad linewidth, but here's how it looks for me (on MacOS). This screenshot shows the output of gnuplot's native aquaterm output:
If we create a png using set term png, the lines become "jumpy" and pixellated:
However, there is a version of the png terminal that uses the Cairo libs for rendering, and that makes the output far more smooth and nicer. set term pngcairo gives this result:
You can use set terminal to check whether this terminal version is available for you. If it is, this should save you conversion work and also give better image quality than a JPG (which is not an ideal format for line art).
The default size of the PNG image generated gnuplot with the PNG terminal is 640x480 pixels. This resolution in certain cases may result in "pixelated" graphs which are not as nice as those produced on screen with the default (wxt) terminal.
You can change the resolution of the output image using the size option:
set terminal png size <x,y>
where x and y are the desired numbers of pixels along the horizontal and vertical axis, respectively.
For example:
set terminal png size 1024,768
Please note that, images with larger resolution will result in proportionally larger files on disk. Alternatively you can try to use non-raster terminals like "post eps" or "pdf" if available on your machine, which may give you high quality scalable and (relatively) portable images without a large disk footprint.
Alternatively, if you want professional (publication-ready) quality images with gnuplot, you should have a look at the epslatex terminal. I have used it extensively for my thesis and my papers with very nice results, virtually no pixelation problems, portability when converting images to pdf, and almost all the capabilities of Latex.

How do I change the background color in gnuplot?

I have a script that renders graphs in gnuplot. The graphs all end up with an ugly white background. How do I change this? (Ideally, with a command that goes into a gnuplot script, as opposed to a command-line option or something in a settings file)
You can change the background color by command set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb"green" behind to set the background color to the the color you specified (here is green).
To get more knowledge about setting the background in gnuplot, you can visit this blog. There are even provided methods to set a gradient color background and background pictures. Good luck!
Ooh, found it. It's along the lines of:
set terminal png x222222 xffffff
It is a setting for some terminal (windows use background). Check out colorbox including its bdefault.
/Allan
According to the official documentation, as of version 5.4 the right way to set the background color in a gnuplot script is something like the following:
set term wxt background rgb "gray75"
Note that the color must be quoted. Beside color names you can use hex values with the format "#AARRGGBB" or "0xAARRGGBB

Resources