GNUPLOT-在饼图中分配颜色 [英] GNUPLOT - Assign colours in pie chart
问题描述
我正在尝试使用GNUPLOT生成饼图,我已经生成了绘图,并且已经使用数组将颜色分配给图例.这是代码,我已经使用了这个问题作为参考
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
当我运行脚本时,我得到一个看起来像这样的output.png
文件
And when I run the script I get a output.png
file that looks like this
output.png
如您所见,饼图颜色与图例颜色不匹配.这是因为我可以通过for
索引轻松设置图例颜色,但是由于要生成饼状图,因此我要遍历every
子句,因此无法获得索引.我也尝试过类似的方法:
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)),\
但是我遇到以下错误:
"gnuplot/piechart.sh",第49行:警告:此绘图样式不适用于6列.设置为xyerrorbars
"gnuplot/piechart.sh", line 49: warning: This plot style does not work with 6 cols. Setting to xyerrorbars
能帮我吗?预先感谢.
已更新
我想我已经迈出了一步.我删除了plot命令的(colour=colour+1)
部分,现在我可以用这种方式设置特定的颜色
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)
在Windows上使用Gnuplot 5.0.0进行了测试.
Tested with Gnuplot 5.0.0 on Windows.
这篇关于GNUPLOT-在饼图中分配颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!