【问题标题】:Is it possible to adjust the height of the boxes plot of gnuplot?是否可以调整 gnuplot 箱线图的高度?
【发布时间】:2020-08-18 03:38:11
【问题描述】:

下面是data.csv

#x,data
0,20
1,30
2,40
3,50

以下代码使用gnuplot绘制方框图并保存为png

import subprocess

proc = subprocess.Popen(['gnuplot','-p'], 
                        shell=True,
                        stdin=subprocess.PIPE,
                        encoding='utf8'
                        )

proc.communicate(
f"""
set terminal png size 400,300; set output 'plot.png';
set boxwidth 1
set style fill solid 1.0 
set xrange [-1:40]
set datafile separator comma 
plot 'data.csv' using 1:2 with boxes notitle

"""
)

输出的png图片:

是否可以修改每个box的高度,设置为10?

预期输出:

【问题讨论】:

  • 你能澄清一下你的问题吗?您想制作具有特定高度的最短盒子,还是希望盒子从低于它们结束位置的特定高度开始?
  • @MatiasAgelvis 我已经编辑了问题以包含预期的输出。输入数据将始终是数字的倍数。在这种情况下,它是 10

标签: gnuplot


【解决方案1】:

使用由伟大的 Hagen Wierstorf 改编自 Object placement using a data file 的脚本。

reset

# The range has to be set manually
set xrange [-1:5]
set yrange [10:70]

set datafile separator comma

set style rectangle dashtype solid fc rgb "#0077ff" fillstyle solid noborder

# Rectangle dimensions
height = 10
width = 1

# --- Read placement from data file
# Set the output of the following plot to a table in order to achieve that it is
# not shown in the current terminal
set table '/dev/null'
# Function to create the right call function
add_rectangle(x,y,hgt,wdt) = sprintf(\
    ' set object rect from "%f", "%f" to "%f", "%f"; ',x,y,x+wdt,y+hgt)
# Initialize command string
CMD = ''
# Do a dummy plot to read the position data
plot 'data.csv' u 1:(CMD = CMD.add_rectangle($1,$2,height,width))
# Execute the drawing command
eval(CMD)
# Restore the terminal
unset table

# dummy empty plot to create the plot instance
plot x with line linecolor rgb"#ffffff" notitle

你可以得到这个情节

据我所知,您无法避免手动设置绘图范围,但由于您使用 python 脚本来调用绘图,因此您可以将列的最小值和最大值传递给脚本并自动设置。

【讨论】:

    【解决方案2】:

    顺便说一句,有with boxxyerror的绘图风格,检查help boxxyerror。 但是,从您的问题和草图以及给定的数据来看,您是否想要并不清楚

    • 3 盒;从一个数据点到下一个数据点(即高度 = 两个连续数据点之间的差异)
    • 4 盒;从固定高度为 10 的数据值开始。

    代码:(第二个选项)

    ### plot boxes with defined height
    reset session
    
    $Data <<EOD
    #x,data
    0,20
    1,30
    2,40
    3,50
    EOD
    
    set xrange [-1:40]
    set datafile separator comma
    set style fill solid 1.0
    plot $Data u 1:2:($1-0.5):($1+0.5):2:($2+10) w boxxyerror notitle
    
    ### end of code
    

    结果:

    【讨论】:

      猜你喜欢
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 2019-09-12
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      相关资源
      最近更新 更多