6
votes

I'd like to get a transparent background in my gnuplot figure. What I coded till now is:

#! /usr/bin/python

from numpy import *
import Gnuplot as gp
import Gnuplot.funcutils

x = (1,2,3)
y=(2,4,5)
x1 = (3,6,8)
g = gp.Gnuplot()
g("set terminal svg size 200,400")
g("set output 'filename.svg'")
g("unset xtics")
g("unset ytics")
g("set multiplot layout 3,1 title 'Title here'")
g("set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb '#ff0000'      behind")
#First
g("set tmargin 2")
g("set title 'Plot'")
g("unset key")
d = gp.Data(x,y,with_="lines")
g.plot(d)
#Secon
g("set title 'Plot2'")
g("unset key")
d1 = gp.Data(x1,y,with_="lines")
g.plot(d1)
#Third
g("set title 'Plot3'")
g("unset key")
d2 = gp.Data(y,x,with_="lines")
g.plot(d2)
g("unset multiplot")

I got a red screen but just the third plot. (Yeah, I have to change the rgb combo to get transparent background color. But, what's it?)

Thanks in advance, FB

1
What operating system are you using -- what svg viewer? The above script works for me on Ubuntu linux with Eye of the Gnome as my viewr (if I remove the set object code). Of course, you can make the background transparent by set object 1 rectangle ... fillstyle transparent solid 0 (0-1 is the level of transparency, 0 being completely transparent) - mgilson
Also, what version of gnuplot are you using? - mgilson
Final question -- Is the end goal to have a given color show up behind all the plots? If so, you can achieve that without transparency in any terminal that supports objects (I can easily modify your script to achieve that). - mgilson
Yeah, thanks for your n-th advice. It is transparent if I use "eog", while it appears not transparent if I use "display" in shell. I'll try on my own script and I'll keep you up. - FrankBr
Just a quick word on display -- display is a wrapper around imagemagick routines which are created (primarily) for raster images. display uses gs (ghostscript) to handle vector graphics (I think) and I've heard that ghostscript doesn't handle transparency correctly on file formats other than postscript (e.g. pdf, maybe svg also ... ). So, I wouldn't trust that what you're seeing in display is what is actually in the file. - mgilson

1 Answers

7
votes

Gnuplot can output transparent backgrounds directly, but not for all formats. For example, this can be done for .png:

set term png transparent truecolor
set output 'plot.png'
plot sin(x)

transparent handles the transparency for the background, and truecolor for transparent objects/plot fillings.

Another way I can get a transparent background out of gnuplot is if I output an .eps and convert it to .png or some transparency-supporting raster format.

Gnuplot example:

set terminal postscript enhanced color
set output 'plot.eps'
plot sin(x)

In bash:

convert plot.eps -rotate 90 plot.png

The resulting .png has a transparent background where the .eps does not. This may have to do with the way imagemagick handles transparency. The same does not happen for me converting .svg to .png; it still has a white background.

You might have some luck using a vector graphics editor such as Inkscape to add transparency.