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.
Related
I am new to gnuplot, and I am trying to plot this data (gnuplot receives this input from stdin):
Regular 5
Block 3
Symbolic 8
Char 3
Socket 7
with this gnuplot code:
set style data histograms
set style fill solid
set terminal png
set output "plot.png"
plot '-' using 2:xtic(1), \
'' using 0:($2 + .1) with labels notitle
I get the error Not enough columns for this style. What am I doing wrong? If I remove the last line with labels, I am able to plot the histogram. How can I modify it to get data labels on top of each histogram bar?
Three columns of information x y text are needed for with labels. You gave coordinates but no actual text. Try
$DATA << EOD
Regular 5
Block 3
Symbolic 8
Char 3
Socket 7
EOD
set style data histograms
set style fill solid
set yrange [0:*]
plot $DATA using 2:xtic(1), \
'' using 0:($2 + .1):2 with labels notitle
Try this:
plot 'input_file' using 2, '' using 0:2:1 with labels offset 0, char 1
Note that I have added the values in a file named input_file and have set set yrange [0:10] to make the plot nicer to watch
This gives:
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:
I am trying to plot some experimental data points (defined by x, y, and z) and and some "iso-z" lines. To plot contour lines, the data need to be in a grid format. I tried to follow proposed elsewhere, where by means of dgrid3d the experimental, nongrid data, are converted by interpolation into grid points, and use these new data points to draw the contour lines.
The script looks as follows:
reset
set term pdf color enhanced font 'Arial, 18' size 13.5cm,9.2cm
set out 'phasediag.pdf'
#Here I convert the data points, stored in 1f.dat into a grid format.
set table '1f-grid.dat'
set dgrid3d 50,50
splot '1f.dat' u 3:4:2
unset table
#Here I would like to draw the countour lines, to be used after in the plot.
set contour base
set cntrparam level auto 5
unset surface
set table 'cont.dat'
splot '1f-grid.dat'
unset table
# Load the palette color definition.
load 'parula.pal'
set logscale cb
set cbrange[1e5:1e9]
set format cb '10^{%1.1T}'
set cblabel 'Mw / g mol^{-1}' offset 2.5,0
set format x '%1.1f'
set format y '%1.1f'
set label '1{/Symbol F}' at 0.4,6.3
set label '2{/Symbol F}' at 2.15,3
plot[0:2.7][2.5:7] '1f.dat' u 3:4:2 with points pt 5 palette, '2f.dat' u 3:2 with points pt 7 lc rgb "black"
set out
Unfortunately, I get the following error when I try to draw the contour lines in the cont.dat file:
Warning: Cannot contour non grid data. Please use "set dgrid3d".
The error surprises me a bit, as the data produced in the first where produced using dgrid3d and are in grid format.
By commentig out the section in which the contour lines should have been drown, I get the following output:
I would simply draw some lines at constant z-values. Any hint for that?
Thanks in advance,
Leo
I'm trying to plot an histogram with a Y logscale. Here is my code:
input_file = "io_time.dat"
output_file = "io_time.eps"
set terminal postscript eps size 4.0,3.5 enhanced color font "Helvetica,18" solid
set output output_file
set style data histogram
set style histogram cluster errorbars gap 1
set boxwidth 0.8
set logscale y
set ylabel 'I/O Duration (sec)'
set xtics mirror rotate by 45 right
plot input_file u 2:3:4:xtic(1) notitle fs pattern 1 lt 1
I'm getting the following error:
"io_time.gp", line 11: label has y coord of 0; must be above 0 for log scale!
Although it seems unrelated to the content of the data file, this data file (io_time.dat) contains 4 columns: the first one for the label of each box, the second one for the box's height (all values are > 0), and the 3rd and 4th columns contain ylow and yhigh values (also > 0).
I'm using gnuplot 4.6 patchlevel 5 installed via MacPort on Mac OS X 10.7.5.
Any idea how to solve this problem?
EDIT: After investigation, it appears that a line in my $HOME/.gnuplot configuration file is causing the problem:
set label textcolor rgb text_color font my_font
text_color and my_font are defined earlier as follows:
text_color = "#000000"
my_font = "Helvetica, 18"
Here is also a sample data file:
A 50.79841091632843 36.28489899635315 69.23793005943298
B 0.11200199127197266 0.032312870025634766 0.42415809631347656
C 0.10992197990417481 0.0323939323425293 0.41459178924560547
D 0.10762600898742676 0.03207087516784668 0.39806699752807617
E 0.03831331729888916 0.03720998764038086 0.04118704795837402
F 0.043952775001525876 0.04250597953796387 0.04720902442932129
G 0.03883504867553711 0.03631401062011719 0.04176783561706543
So you basically already found the source of your problem: You set a label without giving explicit coordinates, in which case 0,0 is used. And this doesn't work with logarithmic scale. Since the labels are placed only when you plot, the line number of the plot command is reported.
BTW: Why do you set an empty label inside a configuration file?
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"