如何修改由GNUPlot创建的饼图 [英] How to modify a Pie Chart created by GNUPlot

查看:82
本文介绍了如何修改由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($2)):(i=i+6) with circle linecolor var

,并以此作为参考.

图表的当前输出:

此代码生成此饼图:

问题:

问题1:如何为RGB格式的图形的每个扇区分配颜色? 第二季度:,有什么方法可以将标签放置在右上角? 第三季度:如何在图表上放置该值?

图表的理想输出:

附录:

借助答案,我进一步改进了代码.答案中的代码对我来说并没有使工作安静,因此我不得不将其调整为:

#!/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($2)):(i) every ::2 with circle linecolor palette,\
      dataname u (mid=(Ai+ang($2)), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.2f\%', $2, perc($2))) 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

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

附录问题:

第四季度:我在标签/标题方面遇到了很大的困难.我已经尝试了答案中的代码,但结果却相同.如何在不互相打印的情况下打印标题?

解决方案

(x):(y):(color),其中x=1.3是固定的,y=i*0.25i=1,2,3,...,STATS_records的内容,而color=ilinecolor palette使用.这些点是填充的正方形(pt 5),长度为4个像素(ps 4).颜色以与各个饼图切片相同的顺序绘制.

每种颜色的标签都可以用以下颜色绘制:

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

其中使用规范的列为(x):(y):(name). x=1.45值稍大于以前使用的值,并且y=i*0.25必须与彩色正方形的值相同. name=$1从第1列提取字符串,该字符串由with labels使用.

注意:您可以使用with labels font 'Arial,10'控制标签的字体和大小.您可以将字体名称省略为font ',10'.

问题1:如何为RGB格式的图形的每个扇区分配颜色?

在上一个命令中,我使用linecolor palette,允许我们使用以下颜色定义颜色

set palette defined (1 1 0.788 0.055, 2 0.090 0.161 0.659)

我在RGB中使用颜色,缩放为[0,1](黄色为1 0.788 0.055;蓝色为0.090 0.161 0.659).

注意:现在应该使用以下方式绘制饼图:

plot for [i=1:STATS_records] dataname u (0):(0):(1):(Ai):(Ai=Ai+ang($2)):(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($2)):(i) every ::i::i with circle linecolor palette,\
     dataname u (mid=(Ai+ang($2)), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.2f\%', $2, perc($2))) 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具有一个gnuplot无法正确读取的标头(第一行Shift,Percentage).我们可以通过注释(添加#字符,例如# Shift,Percentage)或在绘图行中从1开始放置every命令来忽略此行:

plot for [i=1:STATS_records] dataname u (0):(0):(1):(Ai):(Ai=Ai+ang($2)):(i) every ::i::i with circle linecolor palette,\
     dataname u (mid=(Ai+ang($2)), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.2f\%', $2, perc($2))) 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

Input:

I have a myfile.csv file that has the following information:

Shift,Percentage
Day Shift, 39.94
Night Shift, 60.06

GNUPlot Processing:

The myfile.csv file is fed the pie_chart_generator.gnuplot file which is:

#!/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($2)):(i=i+6) with circle linecolor var

and was created with this as a reference.

Current Output of Chart:

This code generates this pie chart:

Questions:

Q1: How can I assign colours to each of the sectors of the graph in RGB format? Q2: Is there a way I can place the the labels on the right hand corner? Q3: How can I place the value on the chart?

Ideal Output of Chart:

Addendum:

I have further improved my code with the aid of the answer. The code in the answer didn't quiet work for me so I had to tweak it to:

#!/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($2)):(i) every ::2 with circle linecolor palette,\
      dataname u (mid=(Ai+ang($2)), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.2f\%', $2, perc($2))) 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

Current Output of Chart from the code above :

Addendum Questions:

Q4: I am having massive difficulty with the labels/titles. I have tried the code in the answer I got the same result. How can I print the titles without printing over each other?

解决方案

The post you cited already suggested a way to place the labels and percentage values. Let me explain the steps, point by point, to achieve this goal. At the end I write the full script.

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

Each slice is defined within two angles (Ai,Af). The percentage values should be placed in the middle of each one, at (x,y)=(0.5*cos(mid), 0.5*sin(mid)), where mid=0.5*(Ai+Af) is the mid-point angle, and 0.5 represents half the radius of the pie-chart.

For the first entry we can set Ai=0, and the angle Af is calculated from your data as Af=ang($2), where ang(x) is defined in your script. For the second entry, we update Ai=Af and calculate again Af=ang($2), and so on.

Finally, the following line should place the percentages:

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

Note: that the first parenthesis (mid=Ai+ang($2), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)) calculates the angles Ai and mid (Af is actually not needed), and then returns the coordinate x=-0.5*cos(mid).

Caveat: both x and y axes must have the same ranges:

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?

For the colored squares, you can plot points at fixed x, and equally spaced y values with:

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

where STATS_records is the number of rows of your file (which you already calculated with stats 'myfile.csv'). This line uses a pseudo file '+', and the using spec has three columns (x):(y):(color), where x=1.3 fixed, y=i*0.25 for i=1,2,3,...,STATS_records, and color=i is used by linecolor palette. The points are filled squares (pt 5) with 4 pixels length (ps 4). The colors are drawn in the same order as the respective pie slices.

The labels of each color can be drawn with:

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

where the using spec has columns (x):(y):(name). The x=1.45 value is slightly greater than that used before, and y=i*0.25 must be the same as those of the colored squares. name=$1 extract the string from column 1, which is used by with labels.

Note: You can control the font and size of the labels with with labels font 'Arial,10'. You can omit the font name as font ',10'.

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

In the last command, I use linecolor palette, allowing us to define the colors with

set palette defined (1 1 0.788 0.055, 2 0.090 0.161 0.659)

where I use the colors in RGB, scaled to [0,1] (yellow-like is 1 0.788 0.055; blue-like is 0.090 0.161 0.659).

Note: The pie-chart should be drawn now with:

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

This is what I obtain with your data

This is the full script:

#!/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($2)):(i) every ::i::i with circle linecolor palette,\
     dataname u (mid=(Ai+ang($2)), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.2f\%', $2, perc($2))) 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

Update: The original myfile.csv has a header (the first line Shift,Percentage) that gnuplot don't read correctly. We can ignore this line by commenting it (add a # character, e.g. # Shift,Percentage), or to place an every command starting at 1 in the plot lines:

plot for [i=1:STATS_records] dataname u (0):(0):(1):(Ai):(Ai=Ai+ang($2)):(i) every ::i::i with circle linecolor palette,\
     dataname u (mid=(Ai+ang($2)), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.2f\%', $2, perc($2))) 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

这篇关于如何修改由GNUPlot创建的饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆