【问题标题】:How do you plot bar charts in gnuplot?你如何在 gnuplot 中绘制条形图?
【发布时间】:2010-09-24 13:30:45
【问题描述】:

如何在 gnuplot 中绘制带有文本标签的条形图?

【问题讨论】:

    标签: graph charts plot gnuplot bar-chart


    【解决方案1】:

    我只想扩展最上面的答案,它使用 GNUPlot 创建条形图,适合绝对的初学者,因为我阅读了答案,但仍然对大量的语法感到困惑。

    我们首先编写一个 GNUplot 命令的文本文件。让我们称之为commands.txt:

    set term png
    set output "graph.png"
    set boxwidth 0.5
    set style fill solid
    plot "data.dat" using 1:3:xtic(2) with boxes
    

    set term png 将设置 GNUplot 输出一个 .png 文件,set output "graph.png" 是它将输出到的文件的名称。

    接下来的两行是不言自明的。第五行包含很多语法。

    plot "data.dat" using 1:3:xtic(2) with boxes
    

    "data.dat" 是我们正在操作的数据文件。 1:3 表示我们将使用 data.dat 的第 1 列作为 x 坐标,使用 data.dat 的第 3 列作为 y 坐标。 xtic() 是负责对 x 轴进行编号/标记的函数。因此,xtic(2) 表示我们将使用 data.dat 的第 2 列作为标签。

    “data.dat”看起来像这样:

    0 label       100
    1 label2      450
    2 "bar label" 75
    

    要绘制图表,请在终端中输入 gnuplot commands.txt

    【讨论】:

      【解决方案2】:

      您可以直接使用 gnuplot 提供的样式直方图。这是一个示例,如果您在输出中有两个文件:

      set style data histograms
       set style fill solid
       set boxwidth 0.5
       plot "file1.dat" using 5 title "Total1" lt rgb "#406090",\
            "file2.dat" using 5 title "Total2" lt rgb "#40FF00"
      

      【讨论】:

        【解决方案3】:

        简单的条形图:

        set boxwidth 0.5
        set style fill solid
        plot "data.dat" using 1:3:xtic(2) with boxes
        

        数据.dat:

        0 label       100
        1 label2      450
        2 "bar label" 75
        

        如果您想以不同的方式设置条形样式,您可以执行以下操作:

        set style line 1 lc rgb "red"
        set style line 2 lc rgb "blue"
        
        set style fill solid
        set boxwidth 0.5
        
        plot "data.dat" every ::0::0 using 1:3:xtic(2) with boxes ls 1, \
             "data.dat" every ::1::2 using 1:3:xtic(2) with boxes ls 2
        

        如果你想为每个条目做多个栏:

        数据.dat:

        0     5
        0.5   6
        
        
        1.5   3
        2     7
        
        
        3     8
        3.5   1
        

        gnuplot:

        set xtics ("label" 0.25, "label2" 1.75, "bar label" 3.25,)
        
        set boxwidth 0.5
        set style fill solid
        
        plot 'data.dat' every 2    using 1:2 with boxes ls 1,\
             'data.dat' every 2::1 using 1:2 with boxes ls 2
        

        如果你想变得棘手并使用一些巧妙的 gnuplot 技巧:

        Gnuplot 具有可用作颜色索引的伪列:

        plot 'data.dat' using 1:2:0 with boxes lc variable
        

        您还可以使用函数来选择您想要的颜色:

        mycolor(x) = ((x*11244898) + 2851770)
        plot 'data.dat' using 1:2:(mycolor($0)) with boxes lc rgb variable
        

        注意:您必须添加一些其他基本命令才能获得与示例图像相同的效果。

        【讨论】:

        • 对于许多应用程序,使用histogram 绘图样式更方便,尤其是对于分组和堆叠值。
        • @Christoph 嗨,如果我想为这种类型的图表添加键?我该怎么办 ?例如,如果我想添加一个键,蓝线是“冬天”,红线是“夏天”
        • @alex 不确定,您指的是所有这些示例中的哪一个:lc rgb variable 您不能有不同的键条目。
        • 无论出于何种原因,我不得不在每个情节子句之后使用指令“notitle”,以阻止传说的出现。
        【解决方案4】:
        使用 2: xtic(1) 和直方图绘制“data.dat”

        这里data.dat包含表单的数据

        标题 1 标题2 3 “长标题” 5

        【讨论】:

          【解决方案5】:

          我推荐 Derek Bruening 的条形图生成器 Perl 脚本。可在http://www.burningcutlery.com/derek/bargraph/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-01-24
            • 1970-01-01
            • 1970-01-01
            • 2013-03-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多