Transparency works only for one set of boxes - gnuplot

So, I am playing around with gnuplot, and it somehow behaves weird. here is the full code:
reset
#set terminal cairolatex pdf input
#set output 'test.tex'
set terminal wxt
poisson(n) = nexp**n/int(n)!*exp(-nexp)
nexp = 3
ax = nexp
ay = 1
bw = 0.2
set xrange[-0.5:12.5]
set samples 13
#set style data boxes
set boxwidth bw absolute
set style fill transparent solid 0.5 border
set xtics 1
set xlabel '$n$'
set ylabel 'Probabiltity of finding $n$ photons'
set key off
plot '+' using (ax+bw/2):(ay) with boxes lc rgb"green" title 'Fock state with $n = 3$',\
'+' using ($0-bw/2):(poisson($0)) with boxes lc rgb"blue" title 'Coherent state with $\langle n \rangle = 3$'
This is the output I get:
For some reason the green box is not transparent in the plot, but transparent in the key. Anyone knows why or how to solve it?
Thanks a lot for any tips.

You generate 13 samples and plot 13 boxes at the sample place. Of course you don't see the transparency anymore. Change the first part of your plot command to draw a single box only:
plot '+' using (ax+bw/2):($0 == 0 ? ay : 1/0) with boxes lc rgb "green"

Related

Gnuplot - How to make multiple plots and save each one to different files with cairolatex

I need to make a 2d plot and a 3d plot, which I want to save as .tex files, respectively labo2-mag/latex/position-x-y-over-time.tex and labo2-mag/latex/position-3d.tex. Is there anything wrong with my script ?
# General settings
set tics font ", 14"
set tmargin at screen 0.7
set key at screen 0.3, 0.8 font ",14" spacing 1.3 samplen 0.8 box opaque
# set key outside font ",14" spacing 1.3 box opaque
# Send the border to the background
set border lw 0.1 back
set xlabel '$t$ [s?]' offset 1.7;
set ylabel '$Position$ [m?]' offset -1;
# Plot once 'in the air'
# set terminal unknown
plot "labo2-mag/data/output.out" using 1:2 with points pointtype 6 lc rgb "red" title '$x(t)$', \
"" using 1:3 with points pointtype 8 lc rgb "green" title '$y(t)$', \
"" using 1:4 with points pointtype 6 lc rgb "blue" title '$z(t)$'
# pause 70
# reread
set terminal cairolatex pdf size 10cm, 10cm
set out 'labo2-mag/latex/position-x-y-over-time.tex'
MAX_Y=GPVAL_Y_MAX
MIN_Y=GPVAL_Y_MIN
MAX_X=GPVAL_X_MAX
MIN_X=GPVAL_X_MIN
set xrange [MIN_X-(MAX_X-MIN_X)*0.1:MAX_X+(MAX_X-MIN_X)*0.01]
set yrange [MIN_Y-(MAX_Y-MIN_Y)*0.05:MAX_Y+(MAX_Y-MIN_Y)*0.05]
replot
# reset terminal
splot "labo2-mag/data/output.out" using 2:3:4 with points pointtype 6 lc rgb "red" title '$pos(t)$'
set terminal cairolatex pdf size 10cm, 10cm
set out 'labo2-mag/latex/position-3d.tex'
replot
Your splot command is issued while the first terminal is still active and writing to the first output file. You need to change the terminal and output before doing a new plot. The minimum would be to issue unset output after each of your replot statements.
You might want or need to also set a new intermediate terminal type together with the unset output. I don't understand the "in the air" comment in the script. Do you mean you don't actually want to plot it? You want to view it interactively before plotting to a file? Otherwise maybe set terminal dumb? If it is relevant, please clarify.

Is it possible to adjust the height of the boxes plot of gnuplot?

Following is the data.csv
#x,data
0,20
1,30
2,40
3,50
The following code uses gnuplot to plot the boxes plot and saves to png
import subprocess
proc = subprocess.Popen(['gnuplot','-p'],
shell=True,
stdin=subprocess.PIPE,
encoding='utf8'
)
proc.communicate(
f"""
set terminal png size 400,300; set output 'plot.png';
set boxwidth 1
set style fill solid 1.0
set xrange [-1:40]
set datafile separator comma
plot 'data.csv' using 1:2 with boxes notitle
"""
)
The output png image:
Is it possible to modify the height of each box and set it to 10?
Expected output:
Using this script adapted from Object placement using a data file, by the great Hagen Wierstorf.
reset
# The range has to be set manually
set xrange [-1:5]
set yrange [10:70]
set datafile separator comma
set style rectangle dashtype solid fc rgb "#0077ff" fillstyle solid noborder
# Rectangle dimensions
height = 10
width = 1
# --- Read placement from data file
# Set the output of the following plot to a table in order to achieve that it is
# not shown in the current terminal
set table '/dev/null'
# Function to create the right call function
add_rectangle(x,y,hgt,wdt) = sprintf(\
' set object rect from "%f", "%f" to "%f", "%f"; ',x,y,x+wdt,y+hgt)
# Initialize command string
CMD = ''
# Do a dummy plot to read the position data
plot 'data.csv' u 1:(CMD = CMD.add_rectangle($1,$2,height,width))
# Execute the drawing command
eval(CMD)
# Restore the terminal
unset table
# dummy empty plot to create the plot instance
plot x with line linecolor rgb"#ffffff" notitle
You can get this plot
As far has I know you can't circumvent setting the plot ranges manually, but since you are using a python script to invoke the plot maybe you can pass the min and max of the columns to the script and automate the setup.
By the way, there is the plotting style with boxxyerror, check help boxxyerror.
However, from your question and your sketch and your given data it is not fully clear whether you want
3 boxes; from one datapoint to the next (i.e. height = difference between two consecutive datapoints)
4 boxes; starting from the data value with fixed height 10.
Code: (second option)
### plot boxes with defined height
reset session
$Data <<EOD
#x,data
0,20
1,30
2,40
3,50
EOD
set xrange [-1:40]
set datafile separator comma
set style fill solid 1.0
plot $Data u 1:2:($1-0.5):($1+0.5):2:($2+10) w boxxyerror notitle
### end of code
Result:

gnuplot histogram label values

I want to put the Y values over the top of bars of the histogram for the graph below:
`
set style data histogram
#clustered
#set terminal wxt enhanced persist
set term post eps enhanced "Times-Roman, 14"
set output 'avg_waste.bmp'
set boxwidth 1.3
set grid
set auto y
set auto x
set style histogram clustered gap 1 title offset 1,0.25
set ylabel "\nAverage Resource Wastage\n\n\n" font "Times-Roman,25"
set xlabel "\nWorkflows\n" font "Times-Roman,25"
#set style fill solid noborder
set style fill pattern border -1
set key at graph 0.2, 0.9
set key spacing 2 font "Times-Roman,18"
set xtics font ", 25"
set ytics font ", 25"
plot for [COL=2:3] 'avg_waste' using COL:xticlabels(1) title columnheader fs pattern 2 `
when I try to use labels, gnuplot gives the error Not enough columns for this style
My data file is
#WASTAGE
CRCH HEFT
Cybershake 20.89 22.5785714286
LIGO 187.3228571429 199.5134285714
SIPHT 205.7514285714 210.3685714286
Montage 12.1485714286 12.7942857143
One possibility would be to place those labels manually as for example:
reset
fontSpec(s) = sprintf("Times-Roman, %d", s)
set term post eps enhanced fontSpec(16)
set output 'avg_waste.eps'
set grid
set auto y
set auto x
ticsFont=fontSpec(16)
set xtics font ticsFont
set ytics font ticsFont
set ylabel "Average Resource Wastage" font fontSpec(25) offset char -1,0
set xlabel "Workflows" font fontSpec(25) offset 0,char -1
set style fill pattern border -1
set style data histograms
set boxwidth 1.0
set style histogram clustered gap 1
keyFont=fontSpec(18)
set key spacing 2 font keyFont
#using directly 'set key spacing 2 font fontSpec(18)' doesn't seem to work...
set key at graph 0.25, 0.9
fn(v) = sprintf("%.1f", v)
plot \
for [COL=2:3] 'avg_waste' using COL:xticlabels(1) title columnheader fs pattern 2, \
'avg_waste' u ($0-1-1./6):2:(fn($2)) w labels font fontSpec(14) offset char 0,0.5 t '' , \
'avg_waste' u ($0-1+1./6):3:(fn($3)) w labels font fontSpec(14) offset char 0,0.5 t ''
Also, since you seem to want to use the titles taken from columnheaders, the script above assumes that the input data is of the form:
WASTAGE CRCH HEFT
Cybershake 20.89 22.5785714286
LIGO 187.3228571429 199.5134285714
SIPHT 205.7514285714 210.3685714286
Montage 12.1485714286 12.7942857143
i.e., the first line is not commented out.
This then produces:
EDIT:
As for the using specification ($0-1-1./6):2:(fn($2)), it's based on the fact that the individual "block" of bars are centered at integer coordinates 0,1,2, and 3. Now, with two blocks per group, the width of each block is 0.3 (2 blocks between the centers of each group + one empty one for the space). The column 0, $0, contains the 0-based index of a particular line in the data file so for example for the "Cybershake" line, it is equal to 1, thus the syntax ($0-1-1./6):2:(fn($2)) then tells Gnuplot to place a label generated by the function fn at coordinates -1./6,$2, i.e., on top of the left bar in the "Cybershake" group. Here, fn is used just as a "macro" to format an input floating point number via the sprintf function.

Bars overlapping box area gnuplot

I have a histogram plot where the bars are simply too high that they overlap the box at the top right that displays information about each bar (what do you call this box?)
Currently it looks like this:
I'm been tring to change the size (height) of the canvas so that it is taller with set size but I couldn't get it to work. I then used yrange but I don't want to specify more range to solve this problem. I want it so that the inner canvas is more square and not rectangular (more height) without changing the range. How do I do this?
set term png
set output 'output.png'
red = "#FF0000";
green = "#00FF00";
blue = "#0000FF";
skyblue = "#87CEEB";
purple = "#4B0082";
set ylabel "Time (s)"
set xlabel "CPU"
set style data histogram
set style histogram cluster
set style fill solid
set boxwidth 0.9
set grid ytics
set xtics rotate
set title "Compiled with gcc/g++"
plot "processor.dat" using 2:xtic(1) title "-O1" linecolor rgb red, \
'' using 3 title "-O2" linecolor rgb blue, \
'' using 4 title "-O3" linecolor rgb green, \
'' using 5 title "-Os" linecolor rgb skyblue, \
'' using 6 title "-Ofast" linecolor rgb purple
The "box" is called "key". In the gnuplot command line, help set key explains its options. You can move it to the empty left upper corner with
set key left
To increase the height of the picture I would change the overall size of the png like this:
set terminal png size 640,640
On my system, your command set term png defaults to set terminal png size 640,480.
To make the plot area an exact square, the following command is used:
set size ratio 1
This is the result with some fake data:

GNUPLOT - Assign colours in pie chart

I am trying generate a pie chart using GNUPLOT, I have already generate the plot, and I have assigned colour to the legend using an array. This is the code, I have used this question as reference
script.sh
#!/usr/bin/gnuplot
set terminal pngcairo nocrop enhanced size 800,400 font "Siemens Sans,8"
set output 'output.png'
filename = 'source.dat'
rowi = 1
rowf = 2
# obtain sum(column(2)) from rows `rowi` to `rowf`
set datafile separator ','
stats filename u 2 every ::rowi::rowf noout prefix "A"
# rowf should not be greater than length of file
rowf = (rowf-rowi > A_records - 1 ? A_records + rowi - 1 : rowf)
angle(x)=x*360/A_sum
percentage(x)=x*100/A_sum
# circumference dimensions for pie-chart
centerX=0
centerY=0
radius=0.2
# label positions
yposmin = 0.0
yposmax = 0.95*radius
xpos = 1.5*radius
ypos(i) = yposmax - i*(yposmax-yposmin)/(1.0*rowf-rowi)
#-------------------------------------------------------------------
# now we can configure the canvas
set style fill solid 1 # filled pie-chart
unset key # no automatic labels
unset tics # remove tics
unset border # remove borders; if some label is missing, comment to see what is happening
set size ratio -1 # equal scale length
set xrange [-radius:2*radius] # [-1:2] leaves space for labels
set yrange [-radius:radius] # [-1:1]
#-------------------------------------------------------------------
pos = 0 # init angle
colour = 0 # init colour
colors = "blue red"
# 1st line: plot pie-chart
# 2nd line: draw colored boxes at (xpos):(ypos)
# 3rd line: place labels at (xpos+offset):(ypos)
plot filename u (centerX):(centerY):(radius):(pos):(pos=pos+angle($2)):(colour=colour+1) every ::rowi::rowf w circle lc var,\
for [i=0:rowf-rowi] '+' u (xpos):(ypos(i)) w p pt 5 ps 4 lc rgb word(colors,i+1),\
for [i=0:rowf-rowi] filename u (xpos):(ypos(i)):(sprintf('%05.2f%% %s', percentage($2), stringcolumn(1))) every ::i+rowi::i+rowi w labels left offset 3,0
The source file to generate the pie chart is this
source.dat
"Tipo","Valor"
"Periodo laboral",723
"Periodo no laboral",81
And when I run the script I get a output.png file that looks like this
output.png
As you can see, the pie chart colours don't fit the legend colour. This is because I can easily set the legend colour thanks to the for index, but for generating the pie chart I am iterating through the every clause so I can't get an index. I have also tried something like:
plot filename u (centerX):(centerY):(radius):(pos):(pos=pos+angle($2)):(colour=colour+1) every ::rowi::rowf w circle lc rgb(word(colors, colour)),\
but I have the following error:
"gnuplot/piechart.sh", line 49: warning: This plot style does not work with 6 cols. Setting to xyerrorbars
Could you help me please? Thanks in advance.
UPDATED
I think I have taken a step. I have removed the (colour=colour+1) part of the plot command, and now I can set an specific color that way
plot filename u (centerX):(centerY):(radius):(pos):(pos=pos+angle($2)):(colour=colour+1) every ::rowi::rowf w circle lc rgb word(colors,colour+1),\
But this draw the pie chart completly blue, and I still need a index or something because it seems colours don't change its value.
Piecewise plotting the pie chart should work:
colorsrgb = "#0000FF #FF0000"
plot for [i=1:rowf-rowi+1] filename u (centerX):(centerY):(radius):(pos):(pos=pos+angle($2)) every ::i::i w circle lc rgb word(colorsrgb, i)
Tested with Gnuplot 5.0.0 on Windows.

Resources