I am trying to plot a heatmap using gnuplot (5.2.8) with the SVG terminal.
Gnuplot interpolates the colors between the center points of the cells.
When I use the pdf terminal the result is as expected and the cells are clear and uniform faces.
How do I turn the interpolation for the SVG terminal off?
#set terminal svg size 600,600
set terminal pdf size 600,600
set style line 12 lc rgb '#ffffff' lt 1 lw 5
set border front 15 ls 12
set grid front ls 12
set datafile separator ','
percent_sample(sample) = sprintf("sample_%d/configuration.csv", sample)
set palette defined (0 '#c00000', 1 '#0000cc') model RGB
unset colorbox
#set cbrange[0:1]
set xrange [-0.5:9.5]
set yrange [-0.5:9.5]
unset key
set tics scale 0
unset xlabel
set xtics 0.5,1
unset ylabel
set ytics 0.5,1
set tmargin 0
set bmargin 0
set lmargin 0
set rmargin 0
#set view map
set output sprintf("sample_conf_%d.pdf", i)
plot percent_sample(i) matrix with image
It is not gnuplot that does the interpolation - it is the SVG viewing program. Leaving aside the question of how you might persuade the viewer not to do this, you can prevent it from happening by using the keyword pixels in the gnuplot command:
plot percent_sample(i) matrix with image pixels
This tells the program to describe each pixel in the output image as a separate rectangle rather than merging them into a stream of contiguous pixels. See also:
heatmaps demo
Related
I am trying to plot a simple linear surface of the equation x + y + 2z = 0. This is my file:
set xrange [-4:4]
set yrange [-4:4]
set zrange [-4:4]
set xlabel 'x'
set ylabel 'y'
set zlabel 'z'
set xyplane at -4.0
unset xzeroaxis
unset yzeroaxis
unset zzeroaxis
set border 1023-128
set xtics out nomirror
set ytics out nomirror
set ztics out
set xtics add ('' -4)
set label 1 "{/:Italic x} + {/:Italic y} + 2{/:Italic z} = 0" at 4,4.2,-2 font 'Times New Roman, 11'
set arrow 1 from 0,0,-4 to 0,0,4 filled
set arrow 2 from 0,-4,0 to 0,4,0 filled
set arrow 3 from -4,0,0 to 4,0,0 filled
set arrow 4 from 3.9,4.1,-2.1 to 3.6,3.6,-2.5
unset key
set pm3d lighting primary 0.5 specular 0.6
set style fill transparent solid 1 noborder
set palette defined (0 "cyan", 1 "green")
unset colorbox
set pm3d depthorder
splot -x/2-y/2 with pm3d
The result
I'm using arrows to show the axis, since they seem to be stuck to the xy plane, and moving it causes further issues with the tics and border. But now the problem is that they are completely occluded by the surface. Is there a setting which allows to appear in front when they "pierce" the surface? I'd like to make the surface semitransparent, but the problem is clearer with these settings.
I guess your original idea, i.e. "piercing" a 3D surface with an arrow or line does not work right away in gnuplot, because gnuplot will not calculate the piercing points automatically. Please correct me if I am wrong and let me know in case there might be a simple solution to this.
As you did in your simple case, you can just split the arrow at the origin, because you already know the piercing point. However, what do you do if the surface is irregular or has several piercing points?
Solution: take the effort to create a segmented 3D arrow and let gnuplot automatically show and hide the surfaces as needed. This is probably getting close to what you had in mind. However, this solution will show surprises when you want to change the color of the arrows. So, there is still room for improvement.
Code: (simple version with arrows just along x,y,z axes)
### arrows "piercing" a 3D surface
reset session
set view equal xyz
set xyplane relative 0.0
set xrange [-4:4]
set yrange [-4:4]
set zrange [-4:4]
# create 3D arrow
r = 0.01 # radius of arrow
rHead = 0.1 # radius of arrrowhead
n = 6 # corners of arrow
set print $myArrow
do for [h=-100:90] {
do for [a=360/n:360:360/n] {
print sprintf("%g %g %g",r*cos(a),r*sin(a), h/100.)
}
print ""
}
do for [h=90:100] {
do for [a=360/n:360:360/n] {
print sprintf("%g %g %g",rHead*(100-h)/10.*cos(a), \
rHead*(100-h)/10.*sin(a), h/100.)
}
print ""
}
set print
unset key
unset colorbox
set pm3d depthorder
set samples 100
set isosamples 100
set view 65,46,1.3
# function to demonstrate "piercing"
f(x,y) = (sin(x*3)/x + sin(y*3)/y - 3)/2
splot \
f(x,y) w pm3d, \
$myArrow u 1:2:($3*4):(0) w pm3d lc rgb var, \
$myArrow u 2:($3*4):1:(0) w pm3d lc rgb var, \
$myArrow u ($3*4):1:2:(0) w pm3d lc rgb var
### end of code
Result:
gnuplot> help layer
A gnuplot plot is built up by drawing its various components in a fixed order.
This order can be modified by assigning some components to a specific layer
using the keywords `behind`, `back`, or `front`. For example, to replace the
background color of the plot area you could define a colored rectangle with the
attribute `behind`.
set object 1 rectangle from graph 0,0 to graph 1,1 fc rgb "gray" behind
The order of drawing is
behind
back
the plot itself
the plot legend (`key`)
front
Within each layer elements are drawn in the order
grid, axis, and border elements
pixmaps in numerical order
So basically you need to add the "front" attribute to the objects you want to appear in front of the plot.
I hope I don't get firebombed here, this is the first time I'm posting. Lol
So after a grueling simulation, I have this large data set of the form r, θ, and T(r,θ). I needed to plot a contour map for this data set. θ is in degrees.
So I ran to GNUPlot and I hoped that this will save me from my problems. I studied a bit about how it works, but I still cannot plot my 2D contour data.
I then researched about my problem then I saw this thread here in SE:
gnuplot 2D polar plot with heatmap from 3D dataset - possible?
I used both codes (that I assume to be working) in that post and NONE is working with my dataset. I hope someone can help me with my problem.
This is what I changed the code into:
reset
set terminal pngcairo size 800,800
set output '3d-polar.png'
set lmargin at screen 0.05
set rmargin at screen 0.85
set bmargin at screen 0.1
set tmargin at screen 0.9
set pm3d map
unset key
set multiplot
# plot the heatmap
set parametric
set isosamples 500
unset border
unset xtics
unset ytics
set angles degree
r = 0.05 # CHANGED THIS
set urange[0:r] # radius
set vrange[0:360] # angle
set xrange[-r:r]
set yrange[-r:r]
set colorbox user origin 0.9,0.1 size 0.03,0.8
splot 'data.dat' using 1:2:3 # CHANGED THIS
# now plot the polar grid only
set style line 11 lc rgb 'white' lw 2
set grid polar ls 11
set polar
set rrange[0:r]
unset raxis
set rtics format '' scale 0
unset parametric
set for [i=0:330:30] label at first (r+0.35)*cos(i), first (r+0.35)*sin(i)\
center sprintf('%d', i)
plot NaN w l
unset multiplot
So I had this little 'eureka' moment. Apparently I had to put dgrid3d and had to convert the coordinates from polar to cartesian.
Upon converting (and putting set dgrid3d) this ugly plot appeared:
reset
set terminal pngcairo size 800,800
set output '3d-polar.png'
set lmargin at screen 0.05
set rmargin at screen 0.85
set bmargin at screen 0.1
set tmargin at screen 0.9
set pm3d map
unset key
set multiplot
# plot the heatmap
set parametric
set isosamples 500
unset border
unset xtics
unset ytics
set angles degree
r = 0.05 # CHANGED THIS
set urange[0:r] # radius
set vrange[0:360] # angle
set xrange[-r:r]
set yrange[-r:r]
set colorbox user origin 0.9,0.1 size 0.03,0.8
set dgrid3d # ADDED THIS
splot 'data.dat' using 1:2:3 # CHANGED THIS
# now plot the polar grid only
set style line 11 lc rgb 'white' lw 2
set grid polar ls 11
set polar
set rrange[0:r]
unset raxis
set rtics format '' scale 0
unset parametric
set for [i=0:330:30] label at first (r+0.35)*cos(i), first (r+0.35)*sin(i)\
center sprintf('%d', i)
plot NaN w l
unset multiplot
The plot.
I'm at my limit here now. Three questions.
1. Am I correct to assume that I can use the mathematics in gnuplot to convert the coordinates from polar to cartesian? Or is there a way for me to use the polar coordinate data immediately?
2. How do I limit the 'colors' to the round polar plot?
3. What happened to the degree and radius readings? Where did they go?
Thanks!
I want to plot a filled circle in Gnuplot, but with transparency changing with the distance to the center of the circle, i.e. a transparency gradient (solid near to the center, and transparent near to the radius of circle).
Is there any way of doing this in Gnuplot?
Thank you in advance!
One option would be to define a custom function describing the gradient (e.g., a Gaussian) and then use pm3d map to visualize it:
set terminal pngcairo size 400,400
set output 'fig.png'
unset border
unset colorbox
unset xtics
unset ytics
set pm3d map
#force the plot to occupy the entire canvas
set lmargin at screen 0
set rmargin at screen 1
set tmargin at screen 1
set bmargin at screen 0
set isosamples 100,100
#specify custom palette
set palette model RGB
set palette defined ( 0 "white", 1 "royalblue" )
set xr [-10:10]
set yr [-10:10]
#a Gaussian might be a reasonable choice
splot exp(-0.2*(x*x+y*y))
This then produces:
Another choice of the defining function might be:
set xr [-pi:pi]
set yr [-pi:pi]
fn(r) = (r>(pi/2))?0:(cos(r))
splot fn(sqrt(x*x+y*y))
This yields:
Try this:
set xrange [0:10]
set yrange [0:10]
do for [i=1:100] {
set style fill transparent solid i/100. noborder
set object circle at 5,5 radius 1.-i/100. fc rgb 'blue'
}
plot -1
Not all terminals support transparency; see help transparent. I made this with the terminal x11.
I would like to plot the inverse of y=xe^x, with dashed lines leading to the point (-exp(-1), -1)
set parametric
set style arrow 1 head filled size char 1.5,20,50
set arrow 1 from -4.1,0 to 4.1,0 heads
set arrow 2 from 0,-4.1 to 0,4.1 heads
set trange[-4:4]
set xrange[-4:4]
set yrange[-4:4]
set xlabel "x"
set ylabel "y"
unset border
set xtics axis format " "
set ytics axis format " "
plot [-4:0.999] log(1-t)/t, t
plot [-4:4] t*exp(t), t lt rgb "black" title '', -exp(-1),t lt rgb "black", t, -1 lt rgb "black"
I would like to restrict the vertical line to running from y=-1 to y=0, and the horizontal line from x=-exp(-1) to x=0. How can this be done?
Also, is there an easier way to set the line colour for all plots, rather than specifying it for each one?
I see, if I'm understanding it correctly you basically need to annotate your graph with the dashed lines, so why don't you use arrows for the same too.
For example:
# I'm using pngcairo dashed terminal
set terminal pngcairo dashed
set output 'graph.png'
set parametric
set style arrow 1 head filled size char 1.5,20,50
set arrow 1 from -4.1,0 to 4.1,0 heads
set arrow 2 from 0,-4.1 to 0,4.1 heads
#set trange[-4:4]
set xrange[-4:4]
set yrange[-4:4]
set xlabel "x"
set ylabel "y"
unset border
set xtics axis format " "
set ytics axis format " "
#plot [-4:0.999] log(1-t)/t, t
set arrow from -exp(-1),-1 to -exp(-1),0 nohead lt 3
set arrow from -exp(-1),-1 to 0,-1 nohead lt 3
plot [-4:4] t*exp(t),t lt rgb "black" title ''
As far as your questions about line colours is concerned, it is also dependent on the type of terminal that you are using. For instance, with the pngcairo terminal, Gnuplot will itself assign different linestyles to plots. If you want specific colours, then offcourse you have to state them. Take a look at this link. You'll find lots of info with a simple search on Gnuplot's linestyles/linecolour/linetypes etc.
I have the following gnuplot script:
#!/bin/bash
gnuplot << EOF
set term postscript portrait color enhanced
set output 'temp.ps'
set border lw 0.2
unset key
set size 1,1
set origin 0,0
set size ratio 1
set size 0.47,0.47
set mxtics 2; set mytics 4
f(x,z)=z+5-5*log10(x)
set style fill transparent solid 0.1
set yrange [12:-2]; set xrange[0:10000]
plot f(x,17.55) w filledcurve lc rgb "black", \
f(x,17.5) w lines lt 2 lc rgb "green"
EOF
Which gives me an output like this:
I need two fix two things in this image:
1- the filled zone has a black line which delimitates it and this should go away
2- the filling is covering the x and y tics and this should not happen
Thanks!
To address 1):
set style fill transparent solid 0.1 noborder
To address 2):
set grid noxtics nomxtics noytics nomytics front
As a side note, transparent in your set style fill command does nothing in the postscript terminal as it doesn't support solid transparency.