使用gnuplot生成饼图

10

我有一个包含以下数据的CSV文件:

name,age
raju,23
anju,34
manju,56
sanju,56

我正在尝试使用gnuplot生成饼图。这是我正在执行的命令:

#!/usr/bin/gnuplot -persist
reset
set terminal wxt
unset key
set datafile separator ","
set xlabel "USERS"
set ylabel "AGE"

plot 'file.csv' using ($0):2:($0):xticlabels(1) with circles lc variable notitle

我做错了什么?


当你说它不起作用时,你是指什么?你得到了什么错误?你已经尝试过什么了? - Nate Barbettini
我正在获得像图中的点一样的圆。 - RAVI507
2个回答

14

显然,Gnuplot目前尚不支持饼图,但我们可以手动绘制。

首先,我们需要获取与数据文件中第二列相关的角度和百分比:

set datafile separator ','
stats 'file.csv' u 2 noout      # get STATS_sum (sum of column 2)

ang(x)=x*360.0/STATS_sum        # get angle (grades)
perc(x)=x*100.0/STATS_sum       # get percentage

然后配置画布:

set size square                 # square canvas
set xrange [-1:1.5]
set yrange [-1.25:1.25]
set style fill solid 1

unset border
unset tics
unset key

并绘制饼图:

Ai = 0.0; Bi = 0.0;             # init angle
mid = 0.0;                      # mid angle
i = 0; j = 0;                   # color
yi  = 0.0; yi2 = 0.0;           # label position

plot 'file.csv' u (0):(0):(1):(Ai):(Ai=Ai+ang($2)):(i=i+1) with circle linecolor var,\
     'file.csv' u (1.5):(yi=yi+0.5/STATS_records):($1) w labels,\
     'file.csv' u (1.3):(yi2=yi2+0.5/STATS_records):(j=j+1) w p pt 5 ps 2 linecolor var,\
     'file.csv' u (mid=Bi+ang($2)*pi/360.0, Bi=2.0*mid-Bi, 0.5*cos(mid)):(0.5*sin(mid)):(sprintf('%.0f (%.1f\%)', $2, perc($2))) w labels

绘图命令中的第一行绘制了饼图,其中列 (0):(0):(1):(Ai):(Ai=Ai+ang($2)):(i=i+1) 包括:

  • 第1-2列: 圆心的x和y坐标
  • 第3列: 圆的半径
  • 第4-5列: 区域的起始和结束角度
  • 第6列: 区域的颜色

绘图命令的第二和第三行放置标签,最后一行将百分比放在每个区域的中间。

结果: Pie chart in Gnuplot

参考文献:(1) Gnuplot surprising (2) Gnuplot tricks


编辑:

基于两个相关问题 (这里这里),提出了一个新脚本:

filename = 'test.csv'

rowi = 1
rowf = 7

# 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=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 space 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 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 u (xpos):(ypos(i)):(sprintf('%05.2f%% %s', percentage($2), stringcolumn(1))) every ::i+rowi::i+rowi w labels left offset 3,0

这段新代码的结果为:新图


1
如果你在图表中只看到标签,请尝试从输入CSV文件中移除第一行(包含标签的行)。 - fracz
1
@LuisGonzález 颜色取自第六列(即带有 colour=colour+1 的列),并且使用标志 lc varlinecolor variable。例如,如果 colour==3,则后者等同于 linecolor 3。默认情况下,最多选择8种颜色进行循环。您可以使用 set linetype 3 linecolor rgb 'red' 等方式自定义线条颜色。 - vagoberto
我尝试复制你发布的前三个代码框中的gnuplot代码,但是它给了我一个警告:Skipping data file with no valid points on the last plot command. 我已经粘贴了完全一样的CSV文件。这里有什么问题? - Nubcake
@Nubcake 我使用 gnuplot 4.6(通常我使用 5.0)重现了这个警告。在绘图命令的第二行中,将 ($1) 替换为 1,使其看起来像这样:u (1.5):(yi=yi+0.5/STATS_records):1 w labels。为什么这样可以解决问题?我不知道 :) - vagoberto
@vagoberto 这是我的错误,我把csv文件中的name,age包含进去了 :) - Nubcake
显示剩余14条评论

0

获得类似结果的另一种方法是预处理数据。

例如,给定

raju,23
anju,34
manju,56
sanju,56

我们可以通过手工计算,使用脚本或电子表格来计算以下内容。
23 / ( 23+34+56+56) * 360
=48.9940828402367
34 / ( 23+34+56+56) * 360
=72.4260355029586
56 / ( 23+34+56+56) * 360
=119.289940828402

并且

48.9940828402367 +72.4260355029586
=121.420118343195
121.420118343195+119.289940828402
=240.710059171597
240.710059171597 +119.289940828402
=359.999999999999

我们现在创建一个类似于数据文件的东西

raju,0,48.9940828402367,1
anju,48.9940828402367,121.420118343195,2
manju,121.420118343195,240.710059171597,3
sanju,240.710059171597,360,4

我们可以将其绘制成如下图所示

set style fill transparent solid 0.9 noborder
plot 'test.csv' using (0):(0):(1):2:3:4 with circles lc var notitle

a pie chart, just 4 blocks of colour, no lables

注意事项:

  1. 这里没有标签的代码,我假设您已经知道如何添加它们。
  2. Gnuplot始终将圆形绘制为圆形,无论x和y轴之间的比率如何。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接