Gnuplot:在Y轴上绘制直方图

3
我正在尝试为以下数据绘制直方图:
<text>,<percentage> 
--------------------
"Statement A",50%
"Statement B",20%
"Statement C",30%

我使用set datafile separator ","来获取相应的列。图应该在X轴上有百分比,在Y轴上有语句(完整的字符字符串)。因此,每个直方图都是水平的。

如何在gnuplot中实现这一点?或者是否有其他绘制好矢量图像的工具可用?

2个回答

6

gnuplot中的histogramboxes绘图样式用于垂直方向的箱形图。要获得水平方向的箱形图,可以使用boxxyerrorbars

对于y轴标签作为字符串,我使用yticlabels将箱子放置在y值0、1和2处(根据数据文件中的行,使用$0访问)。

我让gnuplot将第二列视为数值,这会去掉%。它稍后会在xtics的格式化中添加:

set datafile separator ','
set format x '%g%%'
set style fill solid
plot 'data.txt' using ($2*0.5):0:($2*0.5):(0.4):yticlabels(1) with boxxyerrorbars t ''

版本4.6.4的结果如下: 输入图像描述

谢谢Christoph的帮助。还有一个问题,我如何在每个框的右侧打印百分比?例如,在相应的框的侧面显示37%之类的内容。 - Slayer

0

@Christoph 谢谢。你的回答帮了我。

@Slayer 关于你使用 gnuplot v5.2 patchlevel 6 添加标签的问题,可以参考 @Christoph 提供的示例代码。

示例代码:

# set the data file delimiter
set datafile separator ','

# set the x-axiz labels to show percentage
set format x '%g%%'

# set the x-axis min and max range
set xrange [ 0 : 100]

# set the style of the bars
set style fill solid

# set the textbox style with a blue line colour
set style textbox opaque border lc "blue"

# plot the data graph and place the labels on the bars
plot 'plotv.txt' using ($2*0.5):0:($2*0.5):(0.3):yticlabels(1) with boxxyerrorbars t '', \
     ''          using 2:0:2 with labels center boxed notitle column

提供的样本数据:(plotv.txt)

<text>,<percentage> 
--------------------
"Statement A",50%
"Statement B",20%
"Statement C",30%

参考资料:

  1. gnuplot 5.2演示样例 - 文本框和相关的样例数据


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接