问题描述
我尝试使用GNUPlot在坐标系中绘制两条水平线.两条线代表两个数据集中的平均值.每个数据集具有以下常量: max
, min
和 average
.将相同的逻辑应用于两个数据集,因此我们可以仅关注其中一个.
I try to plot two horizontal lines in a coordinate system using GNUPlot. The two lines represent average values in two datasets. Each dataset has the following constants: max
, min
and average
. The same logic is to be applied to both datasets, so we can simply focus at one of them.
输出应为800 x 800 PNG图像.它们共享X轴,但它们的Y轴在范围值和测量单位方面互不相同.当然,两条线的数值可以任意变化.更准确地说,无论 average
的值如何,我都需要在像素坐标中以y = 300和y = 500绘制两条线.
The output should be a 800 x 800 PNG image. They share the X axis, but their Y axis is different from each other in terms of the ranges' values and unit of measurement. Naturally, the numerical values of the two lines can vary arbitrarily. More precisely, I need to plot the two lines at, say, y = 300 and y = 500 in pixel coordinates, regardless of the value of average
.
据我所知,没有办法告诉GNUPlot在特定像素坐标处绘制某些东西.但是,我确实相信可以通过将范围调整为适当的值来间接实现此目的.在GNUPlot中摸索之后,我设法找到了适当的值.当设置了适当的范围值时,我认为应该很好地绘制集合中的数据点,以使其适合图形.现在,我需要一种通用的方法来处理任何值.
As far I as can tell, there is no way to tell GNUPlot to plot something at a specific pixel coordinate. However, I do believe it is possible to to it indirectly by adjusting the ranges to appropriate values. After poking around in GNUPlot, I managed to find proper values. When the proper range values are set, I think the datapoints in the set should be plotted nicely such that they fit into the graph. Now I need a general approach for any values.
我有以下GNUPlot脚本,其中两个水平线具有任意值:
I have the following GNUPlot script with arbitrary values for two horizontal lines:
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 -p脚本>plot.png
我需要两个看起来像这样的过程:
I need two procedure that looks something like this:
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中的设置yrange中.绿线必须位于 y = 500
,蓝线必须位于 y = 300
(这是 pixel_target_y
参数).任何帮助将不胜感激!
The ranges is put into set yrange in GNUPlot. The green line must be at y = 500
and the blue line must be at y = 300
(this is the pixel_target_y
patameter). Any help is greatly appreciated!
推荐答案
如果我正确理解了您的问题,请尝试用我的话重复一下:您想绘制两个数据集,其中每个数据集的平均值(或均值)在输出图形中具有固定的y像素(或屏幕)位置(与数据值和图形边距无关),对吗?
Let me try to repeat in my words if I understood your question correctly:You want to plot two datasets where the average (or mean) of each datasets have a fixed y-pixel-(or screen) position within the output graph (independent of data values and graph margins), correct?
为此,您需要gnuplot变量 GPVAL_TERM_YMIN
和 GPVAL_TERM_YMAX
.为了获得这些值,您必须先绘制一个虚拟图.然后,您需要进行一些计算以获得适当的范围.您可以从3个不同的图的结果中看到:绿线和蓝线与x标签或图形标题无关.
For this you need the gnuplot variables GPVAL_TERM_YMIN
and GPVAL_TERM_YMAX
. In order to get these values you have to plot a dummy graph first. Then you need to do some calculations to get the proper range.As you can see in the result from 3 different plots: the green and blue lines are independent of x-labels or graph titles.
代码:
### 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
结果:
这篇关于GNUplot-使用窗口坐标系绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!