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

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.

Related

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.

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

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.

Why doesn't the gnuplot grid extend over this whole plot?

Using gnuplot 4.6 patchlevel 1, with the following commands,
set grid linewidth 1 linecolor rgb"#888888"
set datafile separator ","
plot for [n=2:100] "data.csv" using 1:(column(n)) with lines linewidth 2
and the following example data in the file "data.csv",
time,S1,S2
0,0.00015,0
0.1,0.0001357256127053939,1.427438729460607e-005
0.2,0.0001228096129616973,2.719038703830272e-005
0.3,0.0001111227331022577,3.887726689774233e-005
0.4,0.0001005480069053459,4.945199309465411e-005
0.5,9.097959895689501e-005,5.902040104310499e-005
0.6,8.232174541410396e-005,6.767825458589604e-005
0.7,7.448779556871142e-005,7.551220443128858e-005
0.8,6.739934461758323e-005,8.260065538241677e-005
0.9,6.098544896108986e-005,8.901455103891014e-005
1,5.518191617571635e-005,9.481808382428365e-005
the resulting plot looks this:
Question: why does the grid only extend partway from the bottom, and not cover the whole plot? I tried a considerable amount of experimentation with the set xtics and ytics commands, arguments to grid, and more, and have not been able to get the grid to cover the whole plot. What am I missing?
Great question! In fact, the answer is that the grid does cover the whole plot. The problem is that the key is taking over. Try it again, but with an unset key in there before your plot command.
What's happening is that gnuplot is reserving space in the key for all of the columns which have no data. Nothing gets put in the space that was reserved since no reasonable data was found. Ultimately, this pushes the 2 lines that were visible out of the viewable canvas area as well.
I've reproduced this using the x11, png, postscript and pngcairo terminals.
Note that this behavior seems to be version dependent:
With gnuplot 4.4.2 (OS-X, png terminal)
With gnuplot 4.6.0 (OS-X, png terminal)
For those using gnuplot 4.4.4, perhaps there was a bug fix which made it work for gnuplot 4.4.4 and then a regression. It seems to persist into gnuplot 4.7.0 as well. I might file a bug report.

Convert image to indexed color with custom palette through console

I have image.png in truecolor,
palette.png (N colors, where N>256) or text file, where list RGB color palette.
How to get a picture with this palette?
If I use imagemagick:
convert image.png -remap palette.png remap_image.png
It does not work.
convert image.png -map palette.png remap_image.png
Gives a very bad quality. The image is very noisy. File size is bigger than before.
GIMP gives best quality:
Сonvert image to indexed color > use custom palette
But GIMP is GUI. I need to convert a lot images in the console without running the gimp and X.org.
Using a shared palette across multiple images requires a carefully crafted palette. If you don't take great care when using the palette of a single image across many images, the result will be poor.
This needn't be complicated though. If you have accesss to the GIMP (or other tool) which supports truecolor graphics, you can create a large image and fit all of the smaller images into it, then quantize the image to N colors, then use that palette as the source.
you should be able to closely mimic GIMP's behavior in the console using ImageMagick
Once you've got a truecolor image with all the colors you want to quantize,
# Create an 8-bit png from our source, with a 235-color palette as an example.
convert truecolor_source.png -colors 235 palette.png
# Create an 8-bit png from an arbitrary image and use the palette in palette.png
convert sample.png -map palette.png output.png
There are a number of options for down-sampling colors, like dithering. See the ImageMagickv6 example page for an excellent overview with example pictures and code.
Although I still don't exactly understand what you want to do, your currently most recent comment ("Yes, from RGB to palette will set independently. Need set correct quantity of colors"), it sounds like all you want to do is set a strict limit on the amount of colors of a bunch of images, but they don't need to use the same palette.
In that case, the solution is very simple:
convert sample.png -colors 135 output.png
Try playing with the quantization options if the result isn't to your satisfaction.
If the output image is too large for your liking, you can experiment with the -quality option.
If this still isn't satisfactory, please try to explain your goal in a more detailed manner.
Good luck!
cat photo.png | pngnq -s 1 > photoindexed.png
I tend to get good results with the "-remap" (single imge) or "+remap" (multiple images) functions in combination with "-colors". Read up on those functions here. Note that "with "-remap" you provide IM with the final set of colors you want to use for the image, whether you plan to dither those colors, or just replace the ones with their nearest neighbours.", meaning just remapping/replacing might not look good enough, as colors from the input image are simply replaced by those from the palette image. Some form of dithering will be necessary to distribute pixel color conversion errors throughout the output image, because not all colors in the palette match those of the input image.
I'd suggest you use the "-colors N" option for that. This will reduce your output image color count to a maximum of N. By default ImageMagick uses "-dither Riemersma" for this implicitly when you specify "-colors N". The are also other dithering options available.

Resources