I'm running the following script:
#!/bin/bash
gnuplot -e "set t latex ; set size 0.5,1; set output 'grafico.tex';
set xtics ('$\frac{-\pi}{2}$' -pi/2, 0, '$\frac{\pi}{2}$' pi/2); set xrange [-pi/2:pi/2];
set yrange [-2:2]; set ylabel '$ f(x)$'; set xlabel '$\theta$';
plot sin(x); pause -1 'hit return to continue'"
pdflatex latex
atril latex.pdf
Output 1
As a beginner both with programming and gnuplot, I want to be sure that's the correct way to do it. My doubt come from the output, it doesn't looks very good (the resolution, mainly). Is there any simple improvement i'm missing? Any change to improve the plotting technique and enhance the result will be welcome.
Output 2
Changing latex to cairolatex pdf as suggests #Ethan Merritt returns:
which is much better.
gnuplot's original latex terminal, the one you get by saying "set term latex", is horrible by modern standards. Use one of the more recent ones. My favorite is the tikz terminal:
gnuplot>
set term tikz standalone size 5cm, 7cm
set output 'grafico.tex'
set xtics ('$\frac{-\pi}{2}$' -pi/2, 0, '$\frac{\pi}{2}$' pi/2)
set xrange [-pi/2:pi/2]; set yrange [-2:2];
set ylabel '$ f(x)$'; set xlabel '$\theta$';
plot sin(x)
unset output
system("pdflatex grafico")
Notes:
You should probably set the size as part of the set term command. If you use set size your output will contain large blank areas
Your local copy of gnuplot might not support the tikz terminal. In that case you could use set term cairolatex pdf instead, with everything else the same.
gnuplot has other (too many!) latex-based terminals also, but I suggest starting with tikz or cairolatex.
Related
I would like to remove unnecessary margins (gray part in the figure below) in a PDF figure generated by epslatex (gnuplot).
Following are scripts and commands to create the figure.
set term epslatex standalone
set output "figure.tex"
set xlabel "\\LARGE $x$"
set ylabel "\\LARGE $y$"
set format x "\\Large{%.1f}"
set format y "\\Large{%.1f}"
set key left top Left
set size square
set xrange [0.0:1.0]
set yrange [0.0:1.0]
plot x with lines dt 1 lw 5.0 lc rgb "red" title "\\Large $y = x$",\
x*x with lines dt 2 lw 5.0 lc rgb "green" title "\\Large $y = x^2$",\
x*x*x with lines dt 3 lw 5.0 lc rgb "blue" title "\\Large $y = x^3$"
and commands
$ gnuplot sample.gp
$ pdflatex figure.tex
Instead of pdflatex, xelatex would also work. I would like to directly convert to PDF file.
It will be super good if we can remove these margins without too much effort (such as removing the margin manually one-by-one).
Thanks!
If you check help latex, it will tell you that default size is 5 x 3 inches.
Since you set size square, there will be for sure an "unwanted" left and right margin.
What you can do to at least minimize the margins is to set the terminal size to square size as well, e.g. to 3 x 3 inches.
However, keep in mind the graph size is square but x and y axes have tics and labels which require space depending on numbers and labels which might be different for x and y.
set term epslatex standalone size 3 in, 3 in
From help latex:
Syntax:
set terminal {latex | emtex} {default | {courier|roman} {<fontsize>}}
{size <XX>{unit}, <YY>{unit}} {rotate | norotate}
By default the plot will inherit font settings from the embedding
document. You have the option of forcing either Courier (cmtt) or
Roman (cmr) fonts instead. In this case you may also specify a
fontsize. Unless your driver is capable of building fonts at any size
(e.g. dvips), stick to the standard 10, 11 and 12 point sizes.
...
Maybe, there are LaTeX commands to crop the graph to its bounding box.
Thanks to #AlainMarigot help, I change the system to Lua tikz with the option tightboundingbox.
Looks good, but not exactly same as epslatex.
When I use replot in the epslatex terminal, anything I replot is not showing up in the final output.
reset session
set terminal epslatex size 5.0in,4in color colortext font ',10' standalone
set output 'plots.tex'
set size square
plot sin(x)
replot cos(x)
set output
When I use , and plot in the same line, I get correct outputs.
reset session
set terminal epslatex size 5.0in,4in color colortext font ',10' standalone
set output 'plots.tex'
set size square
plot sin(x), cos(x)
set output
replot is very convenient becuase I can use set and change settings.
My question is how to use replot in gnuplot that prints in LaTeX.
In gnuplot both commands are different. Using plot sin(x) followed by replot cos(x) draws two plots: one with sin(x) and another one with the result of the previous plot command (ie sin(x)) and cos(x). Indeed
plot sin(x)
replot cos(x)
is absolutely equivalent to
plot sin(x)
plot sin(x), cos(x)
If you display the 'plots-inc.eps' or if you change the terminal to pdf, you have two pages, the first with the plot of sin(x) and the second with both.
The plots.tex file is supposed to be included in a some tex source and only contains one plot. The actual function that is plotted in the .tex file is the last one (with sin(x) and cos(x)). This code is adapted to dvi output and if you format it with latex plots.tex (and not pdflatex plots.tex) and view the .dvi file with xdvi (or whatever), you can verify that a plot with both functions is present. For unknown reasons, with pdflatex, only the sin(x) appears (but both are in the .tex code and there is the legend for cos(x)).
Here is what I get in the .dvi file:
We can do a zoom on this dvi file.
And compare it to the second plot if we use a pdf terminal.
As you can see, the plot rendering is worse with the dvi file and the function is smoother with the pdf output.
I think the problem is that the epslatex terminal is a very old driver. It was the default in the nineties, but with modern TeX engines, almost no one uses it anymore and I am not sure that it is actively maintained.
This probably explains the problems : the incomplete plot with pdflatex and the lower quality of the rendering. Most probably this driver has never really been adapted to engines like pdflatex or lualatex.
Unless you absolutely need dvi, I would suggest you to switch to another driver. Generate your plots as pdf and use \includegraphics to load them. The plot quality will be increased, but you will also gain all the flexibility of \includegraphics to resize, rotate, clip or do whatever you want to your plots.
Please note that the pdf driver will generate one plot per page and with your initial code there are two plots. You can include a specific page (ie plot) of the pdf with the 'page=' option of \includegraphics, but the simpler is to select different output files per plot.
set output 'plots.pdf'
plot sin(x)
set output 'plots2.pdf'
replot cos(x)
I am using the following script to generate a simple eps image:
set terminal epslatex 8
set output 'sample1.tex'
set size 0.75,0.75
set xrange [-pi:pi]
set yrange [0:1.2]
set xlabel "$x$"
set ylabel "$y$"
plot sin(x)*sin(x) title "$\\sin^2(x)$"
However, when I run it in gnuplot
gnuplot> load "sample.gpi"
I got a blank image, just the grids without numbers are shown.
Any suggestion what I'm doing wrong?
You are probably viewing the sample1-inc.eps file. But you must compile the output LaTeX-file first. For this it is very convenient to use the standalone option and compile from within the script itself:
set terminal epslatex 8 standalone
set output 'sample1.tex'
set size 0.75,0.75
set xrange [-pi:pi]
set yrange [0:1.2]
set xlabel "$x$"
set ylabel "$y$"
plot sin(x)*sin(x) title "$\\sin^2(x)$"
set output # finish the current output file
system('latex sample1.tex && dvips sample1.dvi && ps2pdf sample1.ps')
Now you can load 'sample1.gpi' and view the complete output as sample1.ps or sample1.pdf. If you are on Windows you may need to compile with three separate system calls (just guessing):
system('latex sample1.tex')
system('dvips sample1.dvi')
system('ps2pdf sample1.ps')
I have the following gnuplot plot embedded in my Latex document:
\begin{gnuplot}[terminal=epslatex,terminaloptions={color size 14.5cm, 9cm}]
set view map
unset surface
unset key
unset xtics
unset ytics
unset ztics
set contour base
set cntrparam levels discrete 2,4,8,16,32,64,128,256,512
set isosamples 100
splot y**2 + 0.1*x**2 notitle
\end{gnuplot}
The plot is alright. All I want to achive is that the contour lines all have the same style, i.e. line type and the same colour, black if possible.
Thanks for any advices.
I don't know about the latex terminal, but you could try:
splot y**2 + 0.1*x**2 notitle lc rgb "#000000"
http://www.gnuplot.info/demo/contours.html (see the section where they draw all the contours in the same color -- It's the last example on that page)
EDIT
It looks like the coloring of the contours is controlled by {un}set clabel. So if you just add unset clabel to your script, then the contours should probably show up black (with the lc rgb "#000000" that I had above. Note that unset clabel implies unset key. To achieve this with the ability to add a key is a little more tricky...
in case you ever need to keep the labels...
You'll likely need to set term push to save the current terminal. set term unknown to make the output go nowhere. set table "junk.dat" and then issue your plot commmand as normal. This will write the contours to a file "junk.dat" which can then be splotted with lines after a set term pop to restore the old terminal settings (you'll probably need some variant of title columnhead and maybe an index/every as well to get the labels to appear correctly...) -- I'm not actually sure if the set term push/pop commands are necessary in this case. Anyway, plotting things to tables with gnuplot is something that I've done a number of times for a number of different applications. It's a great tool to keep in mind.
I have gnuplot file that is outputting quite nicely in EPS but I'm suffering some issues when it comes to the labels. For some reason, a random figure (') is being inserted before the each label. This isn't being produced in a PNG export.
Example gnuplot error picture
The gnuplot file is below
set term postscript eps enhanced
set datafile separator ","
set datafile missing "NULL"
set decimal locale
set output 'Average_Costs_Plotted.eps'
set grid
set key left
set ytics nomirror
set xtics nomirror
set format y "£%'.0f"
set ylabel "Total Loss Estimate \n Based on Average Cost Per m^2"
set xlabel "Area Damaged m^2"
plot "Average_Costs_Upto_10000mSQM.csv" using 1:2 axis x1y1 title 'Industrial Processing' w l lw 2
Well that'll teach me to RTFM.
Postscript doesn't support unicode as mentioned above - however there are octal codes for symbols to insert them.
The £ is input by using the code \243