Gnuplot shows garbage when trying to save picture - gnuplot

When I input the following into the gnuplot window:
set xlabel "x"; set ylabel "y";
plot "rk1000.dat" with lines, "teor1000.dat" with lines
The result is a nice plot. However, when I attempt to save it as a .png file, the result is not so nice. After typing
set xlabel "x"; set ylabel "y"; set terminal png size 800,600;
plot "rk1000.dat" with lines, "teor1000.dat" with lines
the result is gnuplot spitting out garbage characters and doing nothing. Here's a screenshot:
How do I fix this? Not even an hour ago I had no problem saving my plots.

You can use set output in your script to make gnuplot redirect the output to a file:
set terminal png size 800,600
set output "plot.png"
plot "rk1000.dat" with lines, "teor1000.dat" with lines
If you are creating png images using gnuplot, the "pngcairo" terminal produces better-looking results. Try set terminal to see the list of available terminals.

Just save the output to a png file:
gnuplot script.gp > picture.png
Search the web for "redirection" if you want to know more.

you should plot first then set term
ex
"plot '../log/test_coverage_${atpg_mode}.log.tmp' title 'Ori ATPG' w lp lt 3 pt 9"
"set terminal png font \"/usr/share/fonts/liberation/LiberationSerif-Italic.ttf\" 9" ; # save the image as the png format
"set terminal png size 1000, 800" ; # save the image size as 800*1000
"set output '${REPORT_DIR}/Test_Coverage_Curve_${atpg_mode}.png'"
"replot"

Related

Inkscape crops eps files generated with gnuplot

I have a script file to generate an eps file with gnuplot. The basics of this script are:
set terminal postcript enhanced color size 30,20 font 'Times-ew-Roman,40'
set xtics -.5,0.125
set ytics 0.1,0.1
set xrange [-0.5,.5]
set yrange [0.,1.6]
set cbrange [-0.5,.5]
set output "file.eps"
plot #whatever i plot
This script generates an eps file, which I can open in ubuntu and I can see is well printed. Now, I want to import this eps into inkscape, but when importing inkscape imports a big frame with only the top left drawn. The rest is blank. Do I have to change anyvalue in my gnuplot script or do something else with inkscape? I tried to open it in inskcape windows and ubuntu versions, and in both cases it happens the same with the same file.
The problem may be that you have not actually asked gnuplot to produce an eps image. Instead you produced a generic PostScript document with a page size that does not match the default. You need to put the keyword "eps" in your terminal command:
set term postscript eps color size 30,20 font "Times-New-Roman,40"

Line spacing changing between 1st and following plots in Gnuplot

If the same label (in a font different than that of the plot) is used in a series of plots, the line spacing in the first of them seems to follow the plot's font, i.e., it's considerably larger or smaller than in the remaining plots:
Setting the key spacing has no influence on this behavior. And this doesn't happen when label and plot fonts are the same.
How can that be avoided?
Here's a minimal code that produces the plots above:
set terminal png size 200,200
set label "1st line\n2nd line" font "monospace, 8"
set output "1.png"
plot x
set output "2.png"
plot x
Gnuplot 5.2 patchlevel 6, under Linux, was used.
This is indeed a strange behaviour. To me this looks like an initialization bug.
If I start the code from the gnuplot command line, it happens only the first time. If you plot again, the line spacing seems to be the correct one.
So, how can this be avoided? Plot it twice! I agree, this is not a really satisfying solution, there must be better ones.
Code:
reset session
set terminal png size 200,200
set label 1 "1st line\n2nd line" font "monospace, 8"
set output "1.png"
plot x
set output "1.png"
plot x
set output "2.png"
plot x
set output
Addition:
Another possibility to workaround might be the following. Set monospace,8 as default for the terminal. But then adjust the font of all other labels like xtics, xlabel, ytics, ylabel, key, ...
Code:
reset session
set terminal png size 200,200 font "monospace, 8"
set label 1 "1st line\n2nd line"
set xtics font "Arial, 12"
set ytics font "Arial, 12"
set key font "Arial, 12"
set output "1.png"
plot x
set output "2.png"
plot x
set output
Result:
As Ethan pointed out in comments:
It is a bug that is specific to the libgd-based terminals (png jpeg gif). You can avoid it by choosing set term pngcairo instead.
The bug has been reported and is already
Fixed in 5.2.8, which will be released by the end the month [Nov 2019].

Show graph on display and save it to file simultaneously in gnuplot

How can I save graph in to file and also print it to display? I've tried:
#!/usr/bin/gnuplot -p
date=system("date +%F_%T | sed 's/:/-/g'")
set term png
set output date.".png"
set term x11
set out
plot sin(x)
PS: Is there a possibility to save the graph which is displayed in gnuplot window? I've noticed that there is copy to clipboard button but no save.
If you want to send a plot both to a file and to an interactive terminal like x11 or wxt you have the replot after you changed the terminal
set terminal png
set output 'file.png'
plot sin(x)
set terminal x11
set output
replot
If you don't want to set the x11 terminal explicitely, but rather use the default terminal, whatever it is, you can use the special terminals push and pop so save and restore a terminal:
set terminal push
set terminal pngcairo
set output 'file.png'
plot sin(x)
set terminal pop
set output
replot
To make this more transparent and save any image after you plotted it to an interactive terminal you could define a gnuplot script export.gp which you can then call and give the output file name as parameter.
The export.gp script is
set terminal push
set terminal pngcairo
set output '$0'
replot
set output
set terminal pop
which you can then use as
plot sin(x)
call 'export.gp' 'test.png'
Note, however, that the exported file and the plot shown in the interactive window will be different, but if you use wxt as interactive and pngcairo or pdfcairo as output terminals, the chances are quite high, that displayed and exported images are very similar.
With gnuplot 5.0 the qt and wxt terminals offer an "Export" button to save exactly the image shown in the window as svg, pdf or png files. Unfortunately, this functionality cannot yet be invoked from a script, i.e. there is no export command.
A good answer was also given in gnuplot - How can I save a graphics file of a plot that is the same as I designed it in xterminal?.
For x11 terminal one can use
system("xwd -id ".GPVAL_TERM_WINDOWID." | convert xwd:- screenshot.png")
It can be also wrapped into a short cut
bind "Ctrl-c" 'system("xwd -id ".GPVAL_TERM_WINDOWID." | convert xwd:- png:- | xclip -sel clip -t image/png")'
So you plot the image
set term x11
plot sin(x)/x
and then press Ctrl+c in the plot window. Just here, I pasted the image with Ctrl+v:
Unfortunately, it does not work for qt or wxt (GPVAL_TERM_WINDOWID is associated to x11). They have clipboard buttons, but snapshots are not scriptable.
Just expanding on Chistofs comprehensive answer
I like to keep the save commands and decide the file name at the end.
So, modifying the answer gave me this
plot sin(x)
set terminal push
set terminal pngcairo
set output 'test.png'
replot
set output
set terminal pop

How to add a pdf image to a gnuplot plot?

I have an image in PDF format and I want to add to an existing gnuplot plot.
My current code is like:
set terminal postscript eps enhanced color solid font "Nimbus Roman No9 L" 35
set output '|epstopdf --filter > Patterns.pdf'
set size 1.8,1.8
set style data histogram
set style histogram cluster gap 1
plot 'Patterns.dat' using ($2/2.90):xtic(1) title col fs pattern 3
and the pdf file is stored in image.pdf.
It can be done, at least with the epslatex terminal. First, for my example, I am going to generate a pdf file which is a gnuplot-generated figure:
set term epslatex standalone
set output "plot1.tex"
plot sin(x)
Now, outside gnuplot, I generate the pdf file (named plot1.pdf):
pdflatex plot1.tex
which looks like this:
To embed this into a gnuplot graph, I use again the epslatex terminal and embed plot1.pdf as I would do with any pdf file in a latex document, using the \includegraphics[]{} environment, with a gnuplot label:
set term epslatex standalone
set output "plot2.tex"
set label at graph 0.75,0.25 '\includegraphics[width=2cm]{plot1.pdf}'
plot x
And, again run pdflatex:
pdflatex plot2.tex
that generates plot2.pdf, which looks like this:
By changing the location of the label you can change the location of the embedded pdf; by changing the width you can change, guess what, the width of the embedded pdf.

bold enhanced text in gnuplot

UPDATE: this issue has been resolved in newer versions (>5.0) of gnuplot; see #andyras' answer.
I am having difficulty getting gnuplot to create labels with bold and enhanced text in non-postscript terminals. The following script
#!/usr/bin/env gnuplot
reset
set terminal pdfcairo enhanced color lw 3 size 3,2 font 'Arial-Bold'
set output 'output.pdf'
set tics scale 0
plot -x title 'normal text', \
-2*x t 'enhanced_{text}', \
-3*x t '{/Arial-Bold attempt to specify_{font}}'
set terminal pngcairo enhanced color lw 3 size 400,300 font 'Arial-Bold'
set output 'output.png'
replot
set terminal postscript enhanced color lw 3 size 6,4 font 'Arial-Bold'
set output 'output.eps'
replot
reset
Produces the following eps (converted to png with convert output.eps -rotate 90 outputeps.png):
which is fine. However, when I use the pdf or png terminals the result looks like this:
Note that while all the label text should be bold, only the label without any enhanced text is bold. In addition, when I try to manually specify the font (last line title) the font is different (reverts to the default?).
Is this behavior I should expect when not using the postscript terminal? Is there another way to specify fonts (i.e. is the naming scheme different outside of postscript)?
Since version 5.0, gnuplot has a new syntax to handle this issue:
"normal text {/Times:Bold boldface-newfont} {/:Italic slanted-default-font } back to normal text"]
These brackets can also be nested.
For Better results in pdf format.
Plot the curves using enhanced eps terminal. Then use Imagemagic to convert your output to pdf format. using the commands
convert myPlot.eps myPlot.pdf
Default resolution with this commands generates a poor output. This can be overcome by using density option with a value of 300. Modified command looks like
convert -density 300 myPlot.eps myPlot.pdf
I found that this preserves all the text formatting of eps file in pdf file.

Resources