GNUplot-使用窗口坐标系绘制线
我尝试使用GNUPlot在坐标系中绘制两条水平线。这两行代表两个数据集中的平均值。每个数据集都有以下常量:GNUplot-使用窗口坐标系绘制线,plot,png,gnuplot,Plot,Png,Gnuplot,我尝试使用GNUPlot在坐标系中绘制两条水平线。这两行代表两个数据集中的平均值。每个数据集都有以下常量:max、min和average。相同的逻辑将应用于两个数据集,因此我们只需关注其中一个 输出应为800 x 800 PNG图像。它们共享X轴,但它们的Y轴在范围值和测量单位方面彼此不同。当然,这两条线的数值可以任意变化。更准确地说,我需要在像素坐标中绘制两条线,比如y=300和y=500,而不管average的值 就我所知,没有办法告诉GNUPlot在特定的像素坐标上绘制一些东西。然而,我相
max
、min
和average
。相同的逻辑将应用于两个数据集,因此我们只需关注其中一个
输出应为800 x 800 PNG图像。它们共享X轴,但它们的Y轴在范围值和测量单位方面彼此不同。当然,这两条线的数值可以任意变化。更准确地说,我需要在像素坐标中绘制两条线,比如y=300和y=500,而不管average
的值
就我所知,没有办法告诉GNUPlot在特定的像素坐标上绘制一些东西。然而,我相信,通过将范围调整到适当的值,可以间接实现这一目标。在GNUPlot中四处搜索之后,我设法找到了合适的值。当设置了适当的范围值时,我认为集合中的数据点应该很好地绘制,以便它们适合图形。现在,我需要一个针对任何值的通用方法
我有以下GNUPlot脚本,其中包含两条水平线的任意值:
set term png size 800, 800
set multiplot layout 1, 1
# Green line
min_green = 0
max_green = 50
set size 1,1
set ytics 20
set yrange [min_green : max_green]
avg_green = 22
plot avg_green linecolor rgb "green"
# Blue line
min_blue = 10
max_blue = 70
set size 1,1
set ytics 20
set yrange [min_blue : max_blue]
avg_blue = 14
plot avg_blue linecolor rgb "blue"
像这样使用:gnuplot-pscript>plot.png
我需要两个类似这样的过程:
range_min = get_new_min_range(pixel_target_y, min, max, avg)
range_max = get_new_max_range(pixel_target_y, min, max, avg)
量程被放入GNUPlot中的设置Y量程。绿线必须在
y=500
处,蓝线必须在y=300
处(这是像素\u目标\u y
参数)。非常感谢您的帮助 如果我正确理解了你的问题,让我试着用我的话重复一下:
您希望绘制两个数据集,其中每个数据集的平均值(或平均值)在输出图形中具有固定的y像素(或屏幕)位置(与数据值和图形边距无关),对吗
为此,您需要gnuplot变量GPVAL\u TERM\u YMIN
和GPVAL\u TERM\u YMAX
。为了获得这些值,必须首先绘制一个虚拟图。然后你需要做一些计算来得到合适的范围。
正如您在3个不同绘图的结果中所看到的:绿色和蓝色线与x标签或图形标题无关
代码:
### have average lines at fixed pixel position within the plot
reset session
myTermSizeX = 800
myTermSizeY = 800
set term pngcairo size myTermSizeX, myTermSizeY
myOutputFile = "Output.png"
set output myOutputFile
myFixY1 = 500
myFixY2 = 300
set title "Some graph title"
set xlabel "x-Axis title"
# create some test data
set table $Data1
plot '+' u 1:(rand(0)*50+40) smooth bezier
unset table
set table $Data2
plot '+' u 1:(rand(0)*40+10) smooth bezier
unset table
stats $Data1 u 2 name 'Data1' nooutput
stats $Data2 u 2 name 'Data2' nooutput
print Data1_min, Data1_mean, Data1_max
print Data2_min, Data2_mean, Data2_max
# dummy plot to get GPVAL_TERM_YMIN, GPVAL_TERM_YMAX
plot x
R_grph1 = real(myFixY1 - GPVAL_TERM_YMIN)/(GPVAL_TERM_YMAX - GPVAL_TERM_YMIN)
R_grph2 = real(myFixY2 - GPVAL_TERM_YMIN)/(GPVAL_TERM_YMAX - GPVAL_TERM_YMIN)
R_data1 = (Data1_mean - Data1_min)/(Data1_max-Data1_min)
R_data2 = (Data2_mean - Data2_min)/(Data2_max-Data2_min)
if (R_data1 > R_grph1) {
Y1min = Data1_min
Y1max = (Data1_mean - Data1_min)/R_grph1 + Data1_min
}
else {
Y1max = Data1_max
Y1min = Data1_max - (Data1_max - Data1_mean)/(1-R_grph1)
}
print Y1min,Y1max
if (R_data2 > R_grph2) {
Y2min = Data2_min
Y2max = (Data2_mean - Data2_min)/R_grph2 + Data2_min}
else {
Y2max = Data2_max
Y2min = Data2_max - (Data2_max - Data2_mean)/(1-R_grph2)
}
print Y2min,Y2max
set yrange [Y1min:Y1max]
set ytics nomirror
set y2range [Y2min:Y2max]
set y2tics nomirror
set output myOutputFile # it seems you have to specify the output again
set key top center
plot \
$Data1 u 1:2 axes x1y1 w lp pt 7 lc rgb "red" ti "Data1", \
Data1_mean axes x1y1 w l lw 2 lc rgb "green" ti "Data1 mean", \
Data1_min axes x1y1 lt 0 not, \
Data1_max axes x1y1 lt 0 not, \
$Data2 u 1:2 axes x1y2 w lp pt 7 lc rgb "orange" ti "Data2", \
Data2_mean axes x1y2 w l lw 2 lc rgb "blue" ti "Data2 mean", \
Data2_min axes x1y2 lt 0 not, \
Data2_max axes x1y2 lt 0 not
set output
### end of code
结果:
### have average lines at fixed pixel position within the plot
reset session
myTermSizeX = 800
myTermSizeY = 800
set term pngcairo size myTermSizeX, myTermSizeY
myOutputFile = "Output.png"
set output myOutputFile
myFixY1 = 500
myFixY2 = 300
set title "Some graph title"
set xlabel "x-Axis title"
# create some test data
set table $Data1
plot '+' u 1:(rand(0)*50+40) smooth bezier
unset table
set table $Data2
plot '+' u 1:(rand(0)*40+10) smooth bezier
unset table
stats $Data1 u 2 name 'Data1' nooutput
stats $Data2 u 2 name 'Data2' nooutput
print Data1_min, Data1_mean, Data1_max
print Data2_min, Data2_mean, Data2_max
# dummy plot to get GPVAL_TERM_YMIN, GPVAL_TERM_YMAX
plot x
R_grph1 = real(myFixY1 - GPVAL_TERM_YMIN)/(GPVAL_TERM_YMAX - GPVAL_TERM_YMIN)
R_grph2 = real(myFixY2 - GPVAL_TERM_YMIN)/(GPVAL_TERM_YMAX - GPVAL_TERM_YMIN)
R_data1 = (Data1_mean - Data1_min)/(Data1_max-Data1_min)
R_data2 = (Data2_mean - Data2_min)/(Data2_max-Data2_min)
if (R_data1 > R_grph1) {
Y1min = Data1_min
Y1max = (Data1_mean - Data1_min)/R_grph1 + Data1_min
}
else {
Y1max = Data1_max
Y1min = Data1_max - (Data1_max - Data1_mean)/(1-R_grph1)
}
print Y1min,Y1max
if (R_data2 > R_grph2) {
Y2min = Data2_min
Y2max = (Data2_mean - Data2_min)/R_grph2 + Data2_min}
else {
Y2max = Data2_max
Y2min = Data2_max - (Data2_max - Data2_mean)/(1-R_grph2)
}
print Y2min,Y2max
set yrange [Y1min:Y1max]
set ytics nomirror
set y2range [Y2min:Y2max]
set y2tics nomirror
set output myOutputFile # it seems you have to specify the output again
set key top center
plot \
$Data1 u 1:2 axes x1y1 w lp pt 7 lc rgb "red" ti "Data1", \
Data1_mean axes x1y1 w l lw 2 lc rgb "green" ti "Data1 mean", \
Data1_min axes x1y1 lt 0 not, \
Data1_max axes x1y1 lt 0 not, \
$Data2 u 1:2 axes x1y2 w lp pt 7 lc rgb "orange" ti "Data2", \
Data2_mean axes x1y2 w l lw 2 lc rgb "blue" ti "Data2 mean", \
Data2_min axes x1y2 lt 0 not, \
Data2_max axes x1y2 lt 0 not
set output
### end of code
您可以使用
设置箭头nohead
来绘制直线。然后,您可以访问不同的坐标系,如相对于画布(窗口大小)的坐标的screen
,或相对于绘图区域的坐标的graph
。很高兴听到这对您有用。如果您的问题得到了回答,请将答案标记为可接受。