如何修改由 GNUPlot 创建的饼图

How to modify a Pie Chart created by GNUPlot

输入:

我有一个 myfile.csv 文件,其中包含以下信息:

Shift,Percentage
Day Shift, 39.94
Night Shift, 60.06

GNUPlot 处理:

myfile.csv 文件被提供给 pie_chart_generator.gnuplot 文件,即:

#!/usr/bin/gnuplot -persist
reset
set title "\n"
set label 1 "My Pie Chart\nShift Usage Break Down" at graph 0,1.125 left
set terminal wxt
unset key
set datafile separator ","
set terminal png 
set size square
set output "piechart.png"
stats 'myfile.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 'myfile.csv' u (0):(0):(1):(Ai):(Ai=Ai+ang()):(i=i+6) with circle linecolor var

并以此创建为

图表的当前输出:

此代码生成此饼图:

问题:

Q1: 如何以 RGB 格式为图形的每个扇区分配颜色? Q2:有什么办法可以把标签放在右上角? Q3:如何在图表上放置值?

图表的理想输出:

附录:

我在答案的帮助下进一步改进了我的代码。答案中的代码对我来说不起作用,所以我不得不将其调整为:

#!/usr/bin/gnuplot -persist
reset

dataname = 'myfile.csv'
set datafile separator ','

# Get STATS_sum (sum of column 2) and STATS_records 
stats dataname u 2 noout

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

# Set Output 
set terminal png
set output "piechart.png"
set size square

# Print the Title of the Chart
set title "\n"
set label 1 "My Pie Chart\nShift Usage Break Down" at graph 0,1.125 left

#set terminal wxt
unset key
set key off

set xrange [-1.5:1.5]
set yrange [-1.5:1.5]
set style fill solid 1

unset border
unset tics
unset colorbox

# some parameters 
Ai = 5.0;                     # Initial angle
mid = 0.0;                    # Mid angle

# This defines the colors yellow~FFC90E, and blue~1729A8
# Set palette defined (1 '#FFC90E', 2 '#1729A8')             # format '#RRGGBB'
set palette defined (1 1 0.788 0.055, 2 0.090 0.161 0.659)   # format R G B (scaled to [0,1])


plot for [i=1:STATS_records] dataname u (0):(0):(1):(Ai):(Ai=Ai+ang()):(i) every ::2 with circle linecolor palette,\
      dataname u (mid=(Ai+ang()), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.2f\%', , perc())) w labels center font ',10',\
      for [i=1:STATS_records]dataname u (1.45):(i*0.25):1 every ::1 with labels left,\
      for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 5 ps 4 lc palette 

# first line plot semicircles: (x):(y):(radius):(init angle):(final angle):(color)
# second line places percentages: (x):(y):(percentage)
# third line places the color labels
# fourth line places the color symbols
unset output

图表的当前输出 来自上面的代码 :

附录问题:

Q4: 我在使用 labels/titles 时遇到了很大的困难。我已经尝试了答案中的代码,我得到了相同的结果。如何打印标题而不相互打印?

post you cited 已经建议了一种放置标签和百分比值的方法。让我逐点解释实现这一目标的步骤。最后我写了完整的脚本。

Q3: How can I place the value on the chart?

每个切片都定义在两个角度内(Ai,Af)。百分比值应放在每个值的中间,在 (x,y)=(0.5*cos(mid), 0.5*sin(mid)) 处,其中 mid=0.5*(Ai+Af) 是 mid-point 角,0.5 代表 [=110] 半径的一半=].

对于第一个条目,我们可以设置 Ai=0,角度 Af 根据您的数据计算为 Af=ang(),其中 ang(x) 在您的脚本中定义。 对于第二个条目,我们更新 Ai=Af 并再次计算 Af=ang(),依此类推。

最后,下一行应放置百分比:

plot 'myfile.csv' u (mid=Ai+ang(), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.2f\%', , perc())) every ::1 w labels center font ',10'

注:第一个括号(mid=Ai+ang(), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid))计算角度AimidAf其实不需要), 然后 returns 坐标 x=-0.5*cos(mid).

警告: x 轴和 y 轴 必须 具有相同的范围:

set xrange [-1.5:1.5]
set yrange [-1.5:1.5]

Q2: Is there a way I can place the the labels on the right hand corner?

对于彩色方块,您可以在固定 x 和等距 y 值处绘制点:

for [i=1:STATS_records] '+' using (1.3):(i*0.25):(i) pt 5 ps 4 linecolor palette

其中 STATS_records 是文件的行数(您已使用 stats 'myfile.csv' 计算)。这一行使用伪文件“+”,using spec 有三列 (x):(y):(color),其中 x=1.3 固定,y=i*0.25 用于 i=1,2,3,...,STATS_recordscolor=ilinecolor palette 使用。这些点是具有 4 个像素长度 (ps 4) 的填充正方形 (pt 5)。颜色的绘制顺序与各个饼图切片的顺序相同。

每种颜色的标签可以用:

plot for [i=1:STATS_records] 'myfile.csv' u (1.45):(i*0.25):1 every ::i::i with labels center font ',10'

其中 using 规范包含列 (x):(y):(name)x=1.45的值比之前用的略大,y=i*0.25必须和彩色方块的值一样。 name= 从第 1 列中提取字符串,该字符串被 with labels 使用。

注意:您可以通过with labels font 'Arial,10'控制标签的字体和大小。您可以省略字体名称,如 font ',10'.

Q1: How can I assign colours to each of the sectors of the graph in RGB format?

在最后一个命令中,我使用 linecolor palette,允许我们使用

定义颜色
set palette defined (1 1 0.788 0.055, 2 0.090 0.161 0.659)

我使用 RGB 颜色,缩放到 [0,1](yellow-like 是 1 0.788 0.055;blue-like 是 0.090 0.161 0.659)。

注意: pie-chart 现在应该绘制为:

plot for [i=1:STATS_records] dataname u (0):(0):(1):(Ai):(Ai=Ai+ang()):(i) every ::i-1::i-1 with circle linecolor palette

这是我从你的数据中得到的

这是完整的脚本:

#!/usr/bin/gnuplot -persist
reset

dataname = 'myfile.csv'
set datafile separator ','

# get STATS_sum (sum of column 2) and STATS_records 
stats dataname u 2 noout    

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

# output 
set terminal png
set output 'piechart.png'
set size square

set title "\n"
set label 1 "My Pie Chart\nShift Usage Break Down" at graph 00.5,0.95 left

set xrange [-1.5:2.5]     # length (2.5+1.5) = 4
set yrange [-2:2]         # length (2+2) = 4
set style fill solid 1

# unset border            # remove axis
unset tics                # remove tics on axis
unset colorbox            # remove palette colorbox 
unset key                 # remove titles

# some parameters 
Ai = 15.0;                # init angle
mid = 0.0;                # mid angle

# this defines the colors yellow~FFC90E, and blue~1729A8
# set palette defined (1 '#FFC90E', 2 '#1729A8')      # format '#RRGGBB'
set palette defined (1 1 0.788 0.055, 2 0.090 0.161 0.659) # format R G B (scaled to [0,1])


plot for [i=1:STATS_records] dataname u (0):(0):(1):(Ai):(Ai=Ai+ang()):(i) every ::i::i with circle linecolor palette,\
     dataname u (mid=(Ai+ang()), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.2f\%', , perc())) ever\
y ::1 w labels center font ',10',\
     for [i=1:STATS_records] dataname u (1.45):(i*0.25):1 every ::i::i with labels left,\
     for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 5 ps 4 lc palette    

# first line plot semicircles: (x):(y):(radius):(init angle):(final angle):(color)
# second line places percentages: (x):(y):(percentage)
# third line places the color labels
# fourth line places the color symbols
unset output

更新: 原来的 myfile.csv 有一个 header (第一行 Shift,Percentage),gnuplot 没有正确读取。我们可以通过注释忽略这一行(添加一个 # 字符,例如 # Shift,Percentage),或者在绘图行中从 1 开始放置一个 every 命令:

plot for [i=1:STATS_records] dataname u (0):(0):(1):(Ai):(Ai=Ai+ang()):(i) every ::i::i with circle linecolor palette,\
     dataname u (mid=(Ai+ang()), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.2f\%', , perc())) ever\
y ::1 w labels center font ',10',\
     for [i=1:STATS_records] dataname u (1.45):(i*0.25):1 every ::i::i with labels left,\
     for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 5 ps 4 lc palette

通过调整和使用 Vagobertos 解释清楚的代码(因为代码对我不起作用)并进一步阅读,我的代码如下:

GNUPlot代码:

#!/usr/bin/gnuplot -persist
reset

dataname = 'myfile.csv'
set datafile separator ','

# Get STATS_sum (sum of column 2) and STATS_records 
stats dataname u 2 noout

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

# Set Output 
set terminal png
set output "piechart.png"
set size square

# Print the Title of the Chart
set title "\n"
set label 1 "My Pie Chart\nShift Usage Break Down" at graph 0,1.125 left

#set terminal wxt
unset key
set key off

set xrange [-1.5:1.5]
set yrange [-1.5:1.5]
set style fill solid 1

unset border
unset tics
unset colorbox

# some parameters 
Ai = 5.0;                     # Initial angle
mid = 0.0;                    # Mid angle

# This defines the colors yellow~FFC90E, and blue~1729A8
# Set palette defined (1 '#FFC90E', 2 '#1729A8')             # format '#RRGGBB'
#set palette defined (1 1 0.888 0.055, 2 0.156 0.455 0.651)   # format R G B (scaled to [0,1])


set palette defined (1 0.961 0.690 0.255, 2 0.180 0.525 0.757)   # format R G B (scaled to [0,1])

plot for [i=1:STATS_records] dataname u (0):(0):(1):(Ai):(Ai=Ai+ang()):(i) every ::i::i with circle linecolor palette,\
      dataname u (mid=(Ai+ang()), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.2f\%', , perc())) w labels center font ',16',\
      for [i=1:STATS_records]dataname u (1.45):(i*0.25):1 every ::i::i with labels left font 'Arial-Bold,10',\
      for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 5 ps 4 linecolor palette 


# ***************************************************   +--------------------         shape code=
#     for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 8 ps 4 linecolor palette          # empty triangle
#     for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 9 ps 4 linecolor palette          # solid triangle
#     for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 7 ps 4 linecolor palette          # solid circle
#     for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 6 ps 4 linecolor palette          # empty circle


# first line plot semicircles: (x):(y):(radius):(init angle):(final angle):(color)
# second line places percentages: (x):(y):(percentage)
# third line places the color labels
# fourth line places the color symbols



unset output

输出饼图: