You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Gnuplot堆叠柱状图中添加基准水平线?

在Gnuplot堆叠柱状图中添加基准水平线

要给你的堆叠柱状图添加一条贯穿整个图表的基准水平线,有两种简单的实现方式,我会结合你的代码详细说明:

方法一:使用set arrow命令(推荐)

这种方式不需要修改现有的plot语句,直接在绘图前定义一条无箭头的线即可,适合不需要给基准线加图例的场景。

修改后的完整代码如下:

# Scale font and line width (dpi) by changing the size! It will always display stretched.
set terminal svg size 600,500 enhanced fname 'arial'
set term png medium background '#ffffff'
set output 'imageslog.png'
set style data histograms
# Key means label...
set key inside top left
set boxwidth 0.5
set ylabel 'size (logscale)'
set ytics ("0" 0, "1B" 1, "10B" 10, "100B" 100, "1KB" 1000, "10KB" 10000, "100KB" 100000,"1MB" 1000000, "10MB" 10000000)
set logscale y
set yrange [0:200000000]
set xtics out 5
set xlabel 'size'
set title 'Peso de la imagen por plataforma'

# 添加基准水平线:这里设置为1MB(对应y值1000000),红色,线宽2
set arrow from graph 0, first 1000000 to graph 1, first 1000000 nohead lc rgb 'red' lw 2

plot 'data.txt' using 2:xtic(1) title "original" with histograms fs pattern 1, \
'data.txt' using 3 title "instagram" with histograms fs pattern 1, \
'data.txt' using 4 title "twitter" with histograms fs pattern 1

关键参数解释:

  • graph 0和graph 1:表示相对于图表区域的x坐标,0是最左端,1是最右端,这样线会自动铺满整个图表宽度,不管你的x轴有多少类别。
  • first 1000000:使用原始数据的y值(对应你设置的1MB刻度),你可以根据需求替换成任意基准值(比如100000对应100KB)。
  • nohead:去掉箭头头,变成一条直线;lc rgb 'red'设置线的颜色;lw 2设置线宽,这些都可以自定义。

方法二:在plot语句中添加水平线(适合需要图例的场景)

如果你想给基准线添加图例,直接在plot的末尾追加一行绘制指令即可:

修改后的plot部分代码如下:

plot 'data.txt' using 2:xtic(1) title "original" with histograms fs pattern 1, \
'data.txt' using 3 title "instagram" with histograms fs pattern 1, \
'data.txt' using 4 title "twitter" with histograms fs pattern 1, \
# 添加带图例的基准线,x范围从0到6覆盖所有类别,y值为1000000
'' using (0):(1000000):(6):(1000000) with vectors nohead lc rgb 'red' lw 2 title "基准线"

说明:

  • (0)和(6)是x轴的起始和结束位置,因为你的x轴有5个类别(x从1到5),设置0到6可以确保线覆盖整个图表的x范围。
  • title "基准线"会把这条线加入到图例中,方便读者识别。

你只需要根据自己的实际基准值替换代码中的1000000,就能得到符合需求的基准线啦。

内容的提问来源于stack exchange,提问作者eljiwo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 07:07:35