Gnuplot,饼图,在左侧放置标签,无法全部看到 [英] Gnuplot, pie chart, placing labels on left, can't see them all

查看:65
本文介绍了Gnuplot,饼图,在左侧放置标签,无法全部看到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Gnuplot制作一些饼图。我在SO上使用了在这里找到的代码。我的数据文件如下:

I need to make some pie charts using Gnuplot. I used the code I found here, on SO. My data file looks like this:

Województwo Suma
Dolnośląskie    3.6 
Kujawsko-Pomorskie  7.5
Lubelskie   4.7
Lubuskie    3.3
Łódzkie 8.1
Małopolskie 6.9 
Mazowieckie 12.5
Opolskie    2.6
Podkarpackie    6    
Podlaskie   3.4
Pomorskie   8
Śląskie 14

我的Gnuplot脚本:

And my Gnuplot script:

#!/usr/bin/gnuplot

set encoding utf8 
set datafile separator "\t"
set termoption enhanced

set terminal epscairo enhanced color dashed rounded size 8.5, 5.5
set output '2008-2015procent_pie.eps'

stats '2008-2015procent_pie.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            # remove axis
unset tics                # remove tics on axis
unset colorbox            # remove palette colorbox 
unset key                 # remove titles

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

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

plot for [i=1:STATS_records] '2008-2015procent_pie.csv' u (0):(0):(1):(Ai):(Ai=Ai+ang($2)):(i) every ::i::i with circle linecolor palette,\
     '2008-2015procent_pie.csv' u (mid=(Ai+ang($2)), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.1f\%', $2, perc($2))) ever\
y ::1 w labels center font ',10',\
     for [i=1:STATS_records] '2008-2015procent_pie.csv' 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

此脚本有2个问题:


  1. 我看不到所有标签,是否可以通过某种方式移动标签可以看到它们吗?

  2. 颜色:在这里,在我的饼图上,我基本上只有两种颜色-黄色和蓝色。如何使它具有多种颜色,不同的颜色代表不同的价值?

我的图表现在看起来像这样:

My chart looks like this now:

谢谢。

-------------------------------------- -------------------------------------------------- -------------------------------------------编辑
- -------------------------------------------------- -------------------------------------------------- -----------------------------

我改变了有点像@RolandSmith所建议的脚本,我也修改了一点数据文件,现在看起来像这样:

I changed a bit my script, as suggested by @RolandSmith, also, I modified a little my data file, Now it looks like this:

Województwo Suma
Dolnośląskie    3.6 
Kujawsko-Pomorskie  7.5
Lubelskie   4.7
Lubuskie    3.3
Łódzkie 8.1
Małopolskie 6.9 
Mazowieckie 12.5
Opolskie    2.6
Podkarpackie    6    
Podlaskie   3.4
Pomorskie   8
Śląskie 14
Świętokrzyskie  2.8  
Warmińsko-Mazurskie 4    
Wielkopolskie   7.9
Zachodniopomorskie  4.6

修改后的脚本:

#!/usr/bin/gnuplot

set encoding utf8 
set datafile separator "\t"
set termoption enhanced

set terminal epscairo enhanced color dashed rounded size 8.5, 5.5
set output '2008-2015procent_pie.eps'

stats '2008-2015procent_pie.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            # remove axis
unset tics                # remove tics on axis
unset colorbox            # remove palette colorbox 
unset key                 # remove titles

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

set palette rgb 33,13,10;

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

现在问题出在标签上-我仍然看不到它们全部。从CSV文件中可以看到,应该有16个标签。我试图更改页面大小,但没有成功。谢谢您的帮助。

Now the problem is with labels - I still can't see all of them. There should be 16 labels, as you can see from the CSV file. I tried to change the page size, with no success. Thank you for help.

当前饼图:

推荐答案


  1. 要向内或向外移动饼图中的标签,请更改 sin cos 前面的 -0.5。要移动标签和颜色方块,请将(i * 0.25)更改为(i * 0.25)-1.2

  1. To move the labels in the pie-chart inwards or outwards, change the "-0.5" in front of the sin and cos. To move the labels and the color-squares, change (i*0.25) to (i*0.25)-1.2 in the third and fourth plot.

更新:更改(i * 0.25)例如(i * 0.18)可缩小标签之间的距离。并将 ps 4 更改为 ps 3 缩小正方形。

Update: Change (i*0.25) to e.g. (i*0.18) to make the distance between the labels smaller. And change ps 4 to e.g. ps 3 to make the squares smaller.

定义较大的面板。您当前的只有两个 条目。获得一种颜色最少的颜色。您可以使用 colorbrewer 生成调色板颜色。

Define a larger palete. Your current one only has two entries. Get one with as least as much colors as you have entries. You could use colorbrewer to generate palette colors.

其他一些点。


  • 虽然这确实很聪明,但您可能将gnuplot way 超出预期范围。考虑使用其他工具,例如 Python的 matplotlib

  • While this is really very clever, you're probably pushing gnuplot way beyond what it was intended. Consider using another tool like e.g. Python's matplotlib.

您的数据加起来不是100,而只有80.6。因此,您应该使用 ang perc 函数适当地缩放图形。我不能用手指指着它,但看起来不正确。

Your data doesn't add up to 100, but only to 80.6. So you should scale your figure properly using the ang and perc functions. I can't put my finger on it, but it doesn't look right.

sprintf ,您只能使用以下百分比: sprintf('%。1f\%',perc($ 2))

In the sprintf, you should only use the percentage:sprintf('%.1f\%', perc($2))

这篇关于Gnuplot,饼图,在左侧放置标签,无法全部看到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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