3
votes

Is there a simple way to plot an (2d) object filled with the palette spectrum?

I just start reading about palettes and pm3d in gnuplot this week. And I'm a little confused.

Is there a simple way to plot an object, for example a rectangle, that's filled with colors in the spectrum of the palette, i.e, an object with the value of fillcolor option given by the spectrum of the palette? Or will I have to use splot?

I couldn't find anything on the internet nor any question about this here...

2
Do you mean an object like a rectangle with a gradient filling, like in powerpoint? I don't think that's a feature of gnuplot--you would have to plot 2D data or a function that have the gradient you want (something like set pm3d map; splot x). - andyras
Yes, just like in powerpoint! After a great time of search on the internet, I think that it isn't a feature of gnuplot too... Could you give an answer, just briefly explaining explaining how to do? Thanks for reading! - Larara

2 Answers

3
votes

If you want a powerpoint-esque filled gradient, you can hack it into gnuplot by having a multiplot where one of the plots is a small rectangular splot:

#!/usr/bin/env gnuplot

set terminal pngcairo enhanced rounded
set output 'gradient.png'

set samples 1000 # for smooth gradient

set multiplot

# plot actual data
plot sin(x)

# set up plot for a rectangle with no decoration
set pm3d map
unset border
unset tics
unset key
unset colorbox
set margin 0

set size 0.2,0.3

# creates a left-to-right gradient
set origin 0.6,0.6
splot x

# creates a top-to-bottom gradient
set origin 0.3,0.3
splot y

# creates a bottom-left to top-right gradient
set origin 0.3,0.6
splot x + y

# and so on
set origin 0.6,0.3
splot x - y

unset multiplot

The result:

enter image description here

For more inspiration, see: http://gnuplot.sourceforge.net/demo/pm3d.html http://www.gnuplotting.org/tag/colormap/

0
votes

After playing a bit with gnuplot, I found another way of plotting the gradient filled rectangles if you have a file with data and that doesn't uses multiplot.

So if you have a file called data with data like this:

x_i y_i

at the i-th column, you can do this at gnuplot:

set view map
set palette
set pm3d explicit map
splot "data" using 1:2:(1) with lines lw 2 lc rgb "your_color", (x<x_min || x>max) || (y<y_min || y>y_max) ? 1/0 : x with pm3d

The important is the explicit option when setting pm3d: it colors with the palette colors when you give the with pm3d command. So you can color your data with your favorite color. The third argument of using is just the z-value, and in this case is 1. The values x_min, x_max, y_min, y_max are the coordinates of the vertices of the rectangle.

As an example, I had a file like this

2*pi*i/500 sin(2*pi*/500)

where pi= 3.1415... With x_min=1, x_max=3, y_min=-0.7 and y_max=0.1, I obtained this graph:

Output of the example above

Of course, this may be rather laborious in comparison with the method given by @andryas, because we have to write that long expression with the ternary operator, but for those unfamiliar with multiplot this works as well.