如何修改 GNUPlot 创建的饼图

2024-05-09

Input:

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

Shift,Percentage
Day Shift, 39.94
Night Shift, 60.06

GNU绘图处理:

The 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

并以此作为创建参考 https://stackoverflow.com/questions/31896718/generation-of-pie-chart-using-gnuplot.

图表当前输出:

This code generates this pie chart: Output .png pie chart

问题:

Q1:如何以 RGB 格式为图表的每个部分指定颜色?Q2:有什么办法可以将标签放在右上角吗?Q3:如何将值放在图表上?

Ideal Output of Chart: enter image description here

附录:

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

#!/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

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

附录问题:

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


The 您引用的帖子 https://stackoverflow.com/questions/31896718/generation-of-pie-chart-using-gnuplot已经建议了一种放置标签和百分比值的方法。让我逐条解释实现这一目标的步骤。最后我写了完整的脚本。

Q3:如何将值放在图表上?

每个切片都在两个角度内定义(Ai,Af)。百分比值应放置在每个值的中间,即(x,y)=(0.5*cos(mid), 0.5*sin(mid)), where mid=0.5*(Ai+Af)是中点角,并且0.5代表饼图半径的一半。

对于第一个条目,我们可以设置Ai=0,和角度Af根据您的数据计算得出Af=ang($2), where ang(x)在您的脚本中定义。 对于第二个条目,我们更新Ai=Af并再次计算Af=ang($2), 等等。

最后,下面的行应该放置百分比:

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:第一个括号(mid=Ai+ang($2), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid))计算角度Ai and mid (Af其实不需要),然后返回坐标x=-0.5*cos(mid).

Caveat:x 轴和 y 轴must具有相同的范围:

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

Q2:有什么办法可以将标签放在右上角吗?

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

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

where STATS_records是文件的行数(您已经使用它计算过stats 'myfile.csv')。该行使用伪文件“+”,并且使用规范有三列(x):(y):(color), where x=1.3 fixed, y=i*0.25 for i=1,2,3,...,STATS_records, and color=i被使用linecolor 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). The x=1.45值略大于之前使用的值,并且y=i*0.25必须与彩色方块相同。name=$1从第 1 列中提取字符串,该字符串用于with labels.

Note:您可以使用以下命令控制标签的字体和大小with labels font 'Arial,10'。您可以省略字体名称font ',10'.

Q1:如何以 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)。

Note:现在应该使用以下命令绘制饼图:

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

Update:原本的myfile.csv有一个标题(第一行Shift,Percentage)gnuplot 无法正确读取。我们可以通过注释来忽略这一行(添加一个#字符,例如# Shift,Percentage),或者放置一个every图中从 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何修改 GNUPlot 创建的饼图 的相关文章

  • gnuplot - 调整键/图例的大小

    如何调整 gnuplot 4 6 0 中图例的大小 我的意思是线条的大小和not只是字体大小 当我从 pdf 切换到 pdfcairo 终端时 大小突然跳跃并导致关键点与兴趣点重叠 我需要 cairo 来启用 unicode 我刚刚发明的答
  • gnuplot/ awk:为过滤后的数据绘制条形图

    我使用 gnuplot 结合 AWK 根据以下输入数据绘制 2D 条形图 Acceptor DonorH Donor Frames Frac AvgDist AvgAng lig 608 O3 HIE 163 HE2 HIE 163 NE2
  • 为同一轴上的抽动设置不同的颜色

    是否可以在同一轴上使用不同颜色或样式的抽动 tics 0 1 1 5 2我想要0和2有色red or bold 非常适合multiplots其中有关于相同测量值的图 并且您希望在不同的图中标记 y 或 x 范围 但又不会使其过载太多 现在对
  • 如何将轴移动到图表中心?

    我需要在原点位于屏幕中心 或中心附近的某个位置 但不一定在中心 的坐标系上绘制一个函数 并且我需要绘制轴 以便它们在原点交叉 轴也应该有标签和抽动以及箭头 我不知道如何有效地做到这一点 到目前为止 在我的代码中 我手动设置了抽动的偏移量 并
  • Macos 上输出中的 gnuplot pdfcairo 未命名 Type 3 字体

    带有 pdfcairo 终端的 Gnuplot 似乎在字体方面给出了奇怪的行为 其中生成的 pdf 具有未命名的 Type 3 字体 以下是 pdffonts 在输出 pdf 文件上的输出 name type encoding emb su
  • 如何创建具有不同 bin 宽度的直方图

    我对其他使用历史情节的人没有成功 一个简单的问题是使用以下数据 age range frequency central band width bin width height respectively 1 4 30 2 5 3 10 5 6
  • GnuPlot 中带零的对数 y

    我的目标是在对数刻度上显示 0 值 略低于 1 我设法绘制了自己的简单直方图 with boxes 具有对数 Y 刻度 我的 Y 值是非负整数 最大为 25000 我无法区分 0 和 1 值 因为 Y 刻度从 1 开始 这在数学上是正确的
  • Gnuplot:将按行和命名的数据绘制为不同颜色和标题的线束

    我正在尝试绘制当前存储的一组图表 我的文件 txt ID01 1 2 3 4 5 ID02 3 4 5 6 7 8 9 ID03 4 3 1 2 3 4 例如 有一行标题为 ID01 仅显示第一行 另一行标题为 ID02 依此类推 如果线条
  • Gnuplot:多个堆叠直方图,每组使用相同的键

    我正在尝试创建一个具有多个堆叠直方图的图 如示例 8here http gnuplot sourceforge net demo histograms html 但对于我的数据来说 每组都有相同的四个类别 如何更改颜色和键 以便每个堆叠列的
  • GNUPLOT 每个直方图条具有不同的颜色

    我想可视化位图文件的不同颜色的数量 我的数据表如下所示 1 163073164 4 185122087 3 255242000 8 255255255 3 000162232 1 181230029 1 127127127 1 136000
  • 使用 Gnuplot 进行时间序列的线性回归

    我是 Gnuplot 的忠实粉丝 我在学习过程中一直将它用于各种项目 最近我想使用 Gnuplot 来绘制一些时间序列图 例如减肥 锻炼结果 气体消耗等 因此我像这样缩放x轴 set xdata time set timefmt d m Y
  • Gnuplot multiplot 中的双柱图

    我尝试创建多图 2x2 和单图的组合 我不知道 我做错了什么 但我不知道该怎么做 我的尝试 plot sin x title this should be a single plot set multiplot layout 2 2 tit
  • gnuplot任意标注x轴

    我有以下 gnuplot 数据文件 1 0 5 0 9 2 0 1 0 5 3 0 7 0 4 其中第一列是 x 轴 第 2 3 列是两个不同的图表 x 轴坐标始终为自然数 它们代表单词在句子中的位置 第 2 列和第 3 列只是对句子中单词
  • 如何在gnuplot中绘制带有彩色边框的矩形

    我想在我的图中画一个空矩形 到目前为止我有 set style rect back fs empty border lt 3 set object 1 rect from 1 1 to 2 2 我有一个带有虚线的矩形 如何更改线条的颜色 l
  • gnuplot 动画 2D 矢量场

    我正在尝试使用 gnuplot 制作 2D 矢量动画 我想显示一行 即一次显示一个向量 我的数据结构如下 它们x y u v 2 24448 0 270645 1 00 1 00 3 24448 0 270645 0 500 1 20 我可
  • 使用Gnuplot时,如何在行标题中打印行的方程?

    我使用 Gnuplot 绘制数据以及线性回归线 目前 该行的 标题 其方程由 Gnuplot 计算 只是 f x 但是 我希望标题是回归线的方程 例如 y mx c 我可以通过从绘图信息输出中读取 m 和 c 来手动执行此操作 然后使用新标
  • gnuplot 中的 output.png 不如提示 shell 中的图好

    我经常绘制图表gnuplot提示 shell 如下所示 gunuplot gt plot sin x with linespoints pointtype 3 出现的数字很棒 今天 我将图表保存在 png文件 像这样 gnuplot gt
  • 将 gnuplot 嵌入现有 QtWidget 中

    我正在用 C 创建一个 伪 实时绘图应用程序 使用 gnuplot 作为绘图后端 我的要求之一是绘图必须位于现有窗口内 而不是有一个单独的绘图窗口 gnuplot 默认为 Gnuplot 有一个选项可以指定 Qt 小部件 ID 这似乎适合我
  • gnuplot 中的块注释

    我有一个很长的 gnuplot 脚本 出于调试目的 我希望能够阻止该脚本的注释部分或使用 goto 语句 这可能吗 我知道我可以使用if陈述 if 1 2 commented out code else non commented out
  • GNUPLOT:尝试提高质量

    如何提高 gnuplot 的质量 看起来这是一个非常低分辨率的图像 这是我正在使用的文件的内容 linkage plot set terminal pdf set out linkage pdf set title Distribution

随机推荐

  • 带括号的上下文管理器

    我试图了解新的新内容带括号的上下文管理器Python 3 10 中的功能 新功能中的顶部项目here https docs python org 3 10 whatsnew 3 10 html 我的测试示例是尝试编写 with open f
  • 在php中的字符串数组中查找字符串的开头[重复]

    这个问题在这里已经有答案了 我知道我们有 php in array 函数 但我正在寻找一种方法来查找以特定字符串开头的字符串数组中的值 例如找到 search string div 1 div 在这样的数组中 array sample gt
  • 如何在 rspec 中模拟实例变量

    我有两节课OneClass and AnotherClass class OneClass def initialize args another member AnotherClass new end def my method if a
  • 变量作为 bash 数组索引?

    bin bash set x array counter 0 array value 1 array 0 0 0 for number in array do array array counter array value array co
  • 如果启用持久化,当数据存在于缓存中时,什么算作读操作?

    如果监听器断开连接超过30分钟 例如 如果用户离线 您将需要支付读取费用 就好像您已经离线一样 发出了全新的查询 如果启用持久性 这仍然适用吗 情况一 App离线时间超过30分钟 启用持久性并从缓存中读取数据 从缓存中读取文档算作读操作吗
  • FieldPath 字段名称不能包含“.”当尝试使用 AGGREGATE 时

    我的查询有什么问题吗 db table aggregate match gt expr gt and gt eq gt size gt events 4 events 0 updated gt lt gt 2019 05 05 我越来越 M
  • Linux TCP服务器:在接受连接之前读取客户端的IP地址

    Related C Winsock API如何在接受连接之前获取连接客户端IP https stackoverflow com questions 716209 c winsock api how to get connecting cli
  • 帮助需要在可选条件下编写正则表达式[关闭]

    我有一个日志文件包含如下内容 log Using data from yyyy mm dd 2011 8 3 0 files queued for scanning Warning E test H ndler pdf File not F
  • 漏洞利用开发 - GETS 和 Shellcode

    试图了解更多有关利用开发和构建 shellcode 的信息 但遇到了一个我不明白背后原因的问题 为什么我无法运行 execve bin sh 等 shellcode 并生成可以与之交互的 shell 另一方面 我可以创建一个反向 bind
  • 实体框架在连接后返回不同的记录

    考虑我们有这两个实体和一个自定义对象 public class Entiy1 public int Id get set public int DestinationId get set public string Name get set
  • CSS 未在 Spring Boot 中加载

    我是 spring 框架工作和 spring boot 的新手 我正在尝试使用 CSS javascript js 添加静态 html 文件 文件结构是 我的 html 文件头看起来像这样
  • 静态文件配置不正确

    我已经在 Heroku 上部署了简单的博客应用程序 它运行在Django 1 8 4 我在静态文件方面遇到了一些问题 当打开我的应用程序时 我看到Application Error页面 所以我尝试调试它并发现当我提交到 Heroku 时它无
  • 用于开发/生产环境的备用 grunt.js 任务

    我真的很希望能够拥有一个开发 grunt 文件并使用相同的文件作为脚本的生产版本 我已经尝试过建议 但是当尝试调用 dev prod 参数时 我的脚本将会失败 我相信答案是针对旧版本的 grunt 或者可能是我正在使用的插件 module
  • Pip install 导致此错误“ cl.exe' failed with exit code 2 ”

    我已经阅读了有关此错误的所有其他问题 但令人沮丧的是 没有一个给出有效的解决方案 如果我跑pip install sentencepiece在命令行中 它给出了以下输出 src sentencepiece sentencepiece wra
  • Swift 4 使用随机密钥解码嵌套 JSON [重复]

    这个问题在这里已经有答案了 我是 Swift 4 的新手 正在尝试从 Wikipedia API 解码此 JSON 我正在努力定义一个结构 因为我发现的所有示例 教程都仅嵌套 1 2 层深度 除此之外 当其中一个密钥是随机的时 如何解码数据
  • 为什么 Internet Explorer 不喜欢这个 jQuery?

    在调试一些在 IE 中不起作用的 jQuery 时 我发现了以下错误消息 var item item itemArray itemIndex find a text trim Object doesn t support this prop
  • AngularJS - 转到上一个/下一个模式

    我正在使用 Angular 构建一个应用程序 其中有一个项目列表 使用 ng repeat 通过单击每个项目 我可以打开一个模式以查看更详细的描述 现在 为了切换到另一个模式 我必须关闭前一个模式 转到列表 然后单击打开另一个模式 我想在单
  • Rails 资产 - 保留许可证注释

    如何防止 Uglifier 删除某些文件中的某些注释 我希望缩小和压缩代码 但我也希望许可注释保持不变 来自 uglifyJS 的文档 nc or no copyright 默认情况下 uglifyjs 将在生成的代码中保留初始评论标记 假
  • 如何制作 HTML/CSS/JS 变色背景(如 Kahoot.it 那样)

    如何使用 html 和 css 以及可能的 javascript 制作类似于 waht 的颜色变化 褪色背景https kahoot it https kahoot it has 你应该学会检查和获取 keyframes bgcolor 0
  • 如何修改 GNUPlot 创建的饼图

    Input 我有一个myfile csv包含以下信息的文件 Shift Percentage Day Shift 39 94 Night Shift 60 06 GNU绘图处理 The myfile csv文件被送入pie chart ge