wish you a good day ! I must plot something like that using matplotlib : enter image description here
The standard ax.bar() function does not easily plot stacked bar charts with negative values, that's why I used dataframe.plot(stacked = True) to plot this bar. The problem is that I need to plot a line plot above this bar chart. This line should use the second vertical axis, so I need to get the current matplotlib axis object used by my dataframe.plot(stacked = True) function, I need to twinx() it and then plot the line on the twined axis.
However these 3 things do not work :
enter image description here
which gives :
enter image description here
enter image description here
which gives :
enter image description here
and
enter image description here
which gives :
enter image description here
I dont know what is going wrong, I know the problem stems from the dataframe.plot(stacked = True) function which is the only one that stacks easily bar charts with negative values.
This only works for me if I do not set a yrange.
Let's say I have a sample-time-overview.csv like
,avg,std,,,TProc,2267.5202096317,4573.0532262204
TParse,4.9922379603,138.6595434301,,,,,
THash,86.4020623229,548.8593468508,,,,,
TEnq,1.1181869688,2.0684998031,,,,,
TInQ,1482.2243626062,4257.8024051927,,,,,
TSend,2253.1871161473,4514.2823125251,,,,,
TWait,1.7578696884,43.1050730747,,,,,
TAnsw,14.3452407932,201.9216484892,,,,,
TProcAll,2269.2780793201,4573.3927526674,,,,,
TTotal,3853.3679320114,7095.0740689587,,,,,
where I am not interested in the first or last two lines.
Basically copy-pasted the code from the link above with minor adjusts:
#!/usr/bin/gnuplot
reset
filename = "sample-time-overview"
set terminal pngcairo size 500,500 enhanced font 'Verdana,10'
set output filename."_piechart.png"
#set title ""
unset border
unset tics
set xrange[-1:1.5]
#uncommend yrange and the plotdisappears
#set yrange[-1.25:1.25]
centerX=0
centerY=0
radius=1
set datafile separator ','
set key off
set style fill solid 1
stats filename.".csv" u 2 every ::1::7 noout prefix "A"
angle(x)=x*360/A_sum
percentage(x)=x*100/A_sum
pos=0.0
colour=0
yi=0
plot filename.".csv" u (centerX):(centerY):(radius):(pos):(pos=pos+angle($2)):(colour=colour+1) every::1::7 w circle lc var
system(sprintf("display %s_piechart.png", filename))
this ends up looking like
I uncomment the yrange and comment the unset border and it looks like this:
which is very annoying because when I then try to add labels ...
plot filename.".csv" u (centerX):(centerY):(radius):(pos):(pos=pos+angle($2)):(colour=colour+1) every::1::7 w circle lc var,\
"" u (1.5):(yi=yi+0.5/A_records):($1) every::1::7 w labels
this will happen:
Which I suspect is due to the missing yrange (because other than that, the code doesn't differ much from what was posted in the linked answer).
How do I get the bloody thing working?
It is better to configure graph properties just before the plot command. Other routines (e.g. stats and thus A_sum) will be affected by these properties (e.g. set yrange).
This is why the pie chart disappears.
Also, be sure to have equal unit lengths for the x and y axes (use set size ratio -1). If not, the circumference will be drawn with respect to the canvas size, and not with respect to the axes. The pie chart will appear cut otherwise (unless an appropriate yrange is given).
With some modifications, I obtain this chart:
This is the code:
filename = 'sample-time-overview'
rowi = 1
rowf = 7
# obtain sum(column(2)) from rows 1 to 7
set datafile separator ','
stats filename.'.csv' u 2 every ::rowi::rowf noout prefix "A"
angle(x)=x*360/A_sum
percentage(x)=x*100/A_sum
# circumference dimensions for pie-chart
centerX=0
centerY=0
radius=1
# 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 place for labels
set yrange [-radius:radius] # [-1:1]
#-------------------------------------------------------------------
pos = 0 # init angle
colour = 0 # init colour
# 1st line: plot pie-chart
# 2nd line: draw colored boxes at (xpos):(ypos)
# 3rd line: place labels at (xpos+offset):(ypos)
plot filename.'.csv' 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 i+1,\
for [i=0:rowf-rowi] filename.'.csv' u (xpos):(ypos(i)):(sprintf('%05.2f%% %s', percentage($2), stringcolumn(1))) every ::i+1::i+1 w labels left offset 3,0
Setting yrange also influences the execution of the stats command. Therefore you should try to set yrange[-1.25:1.25] after the stats command, not before.
PS:
Plotting the labels with
plot filename.'.csv' u (1.5):(yi=yi+0.5/A_records):($1) every::1::7 w labels
does not work for me. I have to remove the dollar sign:
plot filename.'.csv' u (1.5):(yi=yi+0.5/A_records):1 every::1::7 w labels
And I have to adjust the values 1.5 and 0.5 a little bit.
I've been trying to graph several files on the same gnuplot graph using sprintf to read in the filenames. I can read in one argument o.k. if I write:
filename(n) = sprintf("oi04_saxs_%05d_ave_div_sub.dat", n)
plot for [i=1:10] filename(i) u 1:2
then my graph is o.k and I get all files with that argument plotted on the same graph. However I have a string of characters that changes near the end of my filename and when I try to reflect this in
filename(n,m) = sprintf("oi04_saxs_%05d_0001_%04s_ave_div_sub.dat",n,m)
plot for [i=1:10] filename(i,m) u 1:2
I get the following error message: 'undefined variable m'. I've tried removing the loop and just running
plot for filename(m)
and this results in the same error message. Help in understanding what's going wrong and how to fix it would be much appreciated :)
This is my full script:
unset multiplot
reset
set termoption enhanced
set encoding utf8
set term pdf size 18cm,18cm font 'Arial'
set pointsize 0.25
set output 'StoppedFlowResults.pdf'
set logscale
set xlabel '{/:Italic r} / [Q]'
set ylabel '{/:Italic Intensity}'
filename(n) = sprintf("./Result_curve-%d.txt/estimate.d", n)
myColorGradient(n) = sprintf("#%02x00%02x", 256-(n-1)*8-1, (n-1)*8)
set key off
set multiplot layout 2,1
filename(n,m) = sprintf("oi04_saxs_%05d_0001_%04s_ave_div_sub.dat",n,m);
plot for [i=1:10] filename(i,m) u 1:2 not
unset multiplot
set output
based on help for, you can have nested iterations e.g.:
plot for [i=1:3] for [j=1:3] sin(x*i)+cos(x*j)
In your case you can mix this with strings (you have yet to define string possible values) with something like:
plot for [i=1:3] for [m in "A B C D"] filename(i,m) u 1:2 not
Since SF seems to have further problems it is kind of difficult to find answers to this question. I already know how to plot 2 diagrams with (set) multiplot. But how can I draw some vertical lines going over both diagrams. eg.
set parametric
set trange [a:b]
plot 16,t
I have no clue how to do this over two plots.
The plot should look like this: http://i.stack.imgur.com/90Uue.png
Usually, I use set arrow ... nohead to draw vertical lines. In order to plot them over two plots, I would use screen coordinates for the y-value:
set multiplot layout 2,1
set xrange [0:300]
set arrow 1 from first 16,screen 0.05 to first 16, screen 0.95 nohead lc rgb 'red' lw 2
set arrow 2 from first 256,screen 0.05 to first 256, screen 0.95 nohead lc rgb 'red' lw 2
plot x
unset arrow
plot 2*x
unset multiplot
Here is a code snippet that illustrates how to do it (though it needs some tuning in alignment).
set multiplot
# plot 1
set origin 0,0
set size 1,.5
set xrange [0:2*pi]
set yrange [-1.1:1.1]
plot sin(x)
# plot 2
set origin 0,.5
set size 1,.5
plot cos(x)
# line
set origin 0,0
set size 1,1
unset xtics
unset ytics
unset xlabel
unset ylabel
unset border
set parametric
plot 1,t
unset multiplot
I'm trying to plot a histogram for the following data:
<text>,<percentage>
--------------------
"Statement A",50%
"Statement B",20%
"Statement C",30%
I used the set datafile separator "," to obtain the corresponding columns. The plot should have percentage on the X-axis and the statements on the Y-axis (full character string). So each histogram is horizontal.
How can I do this in gnuplot?
Or is there other tools for plotting good vector images?
The gnuplot histogram and boxes plotting styles are for vertical boxes. To get horizontal boxes, you can use boxxyerrorbars.
For the strings as y-labels, I use yticlabels and place the boxes at the y-values 0, 1 and 2 (according to the row in the data file, which is accessed with $0).
I let gnuplot treat the second column as numerical value, which strips the % off. It is added later in the formatting of the xtics:
set datafile separator ','
set format x '%g%%'
set style fill solid
plot 'data.txt' using ($2*0.5):0:($2*0.5):(0.4):yticlabels(1) with boxxyerrorbars t ''
The result with version 4.6.4 is:
#Christoph Thank you. Your answer helped me.
#Slayer Regarding your question to add labels using gnuplot v5.2 patchlevel 6 and using #Christoph's provided sample.
Sample Code:
# set the data file delimiter
set datafile separator ','
# set the x-axiz labels to show percentage
set format x '%g%%'
# set the x-axis min and max range
set xrange [ 0 : 100]
# set the style of the bars
set style fill solid
# set the textbox style with a blue line colour
set style textbox opaque border lc "blue"
# plot the data graph and place the labels on the bars
plot 'plotv.txt' using ($2*0.5):0:($2*0.5):(0.3):yticlabels(1) with boxxyerrorbars t '', \
'' using 2:0:2 with labels center boxed notitle column
Sample Data Provided:(plotv.txt)
<text>,<percentage>
--------------------
"Statement A",50%
"Statement B",20%
"Statement C",30%
Reference(s):
gnuplot 5.2 demo sample - textbox and the related sample data
gnuplot