【问题标题】:Pie chart gnuplot饼图 gnuplot
【发布时间】:2022-02-04 17:23:54
【问题描述】:

有人在 gnuplot 中有饼图的示例代码吗?我找不到任何很好的例子,它有一个简单的图表和它周围的文字,其中的 % 符号很容易显示每个部分有多少圆圈。

一些示例数据:

Management frames   4596
Control frames  70173
Data frames 40347
TCP packets 36864
HTTP packets    525
ICMP packets    47
Total frames    115116

【问题讨论】:

  • 你检查过这个吗? stackoverflow.com/questions/31896718/…
  • @theozh 似乎真的很难,我没有那种格式的列,它们用于他们的 csv 文件。我有类别以及总数中的多少。
  • 好吧,那么您的输入数据是什么样的呢?请提供几行示例!
  • @theozh 管理帧 4596 控制帧 70173 数据帧 40347 TCP 数据包 36864 HTTP 数据包 525 ICMP 数据包 47 总帧数 115116
  • 您知道您可以编辑您的问题以添加更多信息。这次我为你做了。这已经是一些信息,但仍然太少。你想绘制帧或数据包或两者都绘制什么......?数据文件中的列分隔符、空格或制表符或逗号是什么?

标签: graph gnuplot


【解决方案1】:

更新: 几年后,我再次看到这篇文章,我觉得代码看起来很乱。因此尝试改进和清理它。

以下代码与我上面引用的链接有点不同。

  • 而不是单独列表中的预定义颜色序列或数字代码,部分的颜色在项目/编号旁边的数据块(或数据文件)中由 gnuplot 中预定义颜色的名称给出(另见@987654321 @)。因为使用了调色板,所以您可以输入颜色名称或十六进制代码,例如magenta0xff00ff

  • 标签对齐leftright,具体取决于它们相对于0 的位置。

  • 您可以通过PieStart 选择起始角度,通过PieDirection 选择饼图的“旋转”方向

  • 您可以为线段和标签添加单独的径向和角度偏移

  • 如您所见,原始数据中不需要总和。总和将自动计算。

  • 我定义各种实际上不依赖n的函数f(n)的原因是为了获取其他变量的当前值(在调用函数时)而不是向函数传递大量参数.

我希望您可以根据需要调整此代码。

代码:(适用于 gnuplot>=5.0.0)

### pie-chart with labels with gnuplot
reset session

set size square
set angle degrees
set border 0
unset colorbox
unset tics
unset key 

$Data <<EOD
# label     value   color      SRoff   SAoff   LRoff   LAoff
"Alpha"     85843   red          0       0      0      0
"Beta"      44000   green        0.2    45      0.2    0
"Gamma"     25399   blue         0       0      0      0
"Delta"     18451   magenta      0       0      0      0
"Epsilon"   12344   yellow       0       0      0      0
"Zeta"      11999   cyan         0       0      0      0
"Eta"       9000    orange       0       0      0      0
"Theta"     8500    0xb0f060     0       0      0.03   0
"Iota"      4711    dark-violet  0       0      0.12   0
EOD

colLabel   = 1     # label
colValue   = 2     # segment value
colColor   = 3     # segment color, either color name or 0xRRGGBB value
colSegRoff = 4     # radial  segment offset
colSegAoff = 5     # angular segment offset
colLabRoff = 6     # radial  label   offset
colLabAoff = 7     # angular label   offset

# define a palette from colornames of the datafile/datablock in column colColor
set table $Dummy
    myPalette = ''
    plot $Data u (myPalette = myPalette.(myPalette eq '' ? '' : ', ').sprintf('%d "%s"',$0,strcol(colColor)),$0) with table
    myPalette = '('.myPalette.')'
unset table
set palette defined @myPalette

stats $Data u colValue nooutput    # get total sum from column colValue
TotalSum = STATS_sum

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

PieStart       = 90      # 0 = 3 o'clock, 90 = 12 o'clock
PieDirection   = -1      # -1 clockwise, 1 counterclockwise
Radius         = 1.0
RadiusLabelOff = 0.05    # default radial label offset
SegPosX(n)     = column(colSegRoff)*cos((a2+a1+column(colSegAoff))*0.5)
SegPosY(n)     = column(colSegRoff)*sin((a2+a1+column(colSegAoff))*0.5)
LabPosX(n)     = (Radius+RadiusLabelOff+column(colLabRoff))*cos((a2+a1+column(colLabAoff))*0.5)
LabPosY(n)     = (Radius+RadiusLabelOff+column(colLabRoff))*sin((a2+a1+column(colLabAoff))*0.5)
a1=a2=PieStart
getAngles(n)   = (a1=a2, a2=a2+sgn(PieDirection)*column(colValue)/TotalSum*360)
getLabel(n)    = sprintf("%s %.1f%%", strcol(colLabel),  column(colValue)/TotalSum*100)

set multiplot layout 2,1

    plot $Data u (getAngles(0), SegPosX(0)):(SegPosY(0)):(Radius):(PieDirection<0?a2:a1):(PieDirection<0?a1:a2):($0) \
           with circles fs solid 1.0 lc palette notitle,\
        '' u ( getAngles(0), Align=LabPosX(0)):(LabPosY(0)): (Align>0? getLabel(0) : '') with labels font ",10" left, \
        '' u ( getAngles(0), Align=LabPosX(0)):(LabPosY(0)): (Align<0? getLabel(0) : '') with labels font ",10" right

    PieDirection = +1
    a1=a2=PieStart
    replot

unset multiplot
### end of code

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多