Gnuplot互换轴 [英] Gnuplot interchanging Axes
问题描述
我想用gnuplot复制此图:
I would like to reproduce this plot with gnuplot:
我的数据具有以下格式:
My data has this format:
数据
1:时间
2:价格
3:音量
Data
1: time
2: price
3: volume
我尝试过:
plot file using 1:2 with lines, '' using 1:3 axes x1y2 with impulses
给出正常时间序列图,以y1
作为价格,y2
作为数量.
接下来,我尝试了:
Which gives a normal time series chart with y1
as price and y2
as volume.
Next, I tried:
plot file using 2:1 with lines, '' using 2:3 axes x1y2 with impulses
哪个给出价格序列,其中y1
作为时间,y2
作为数量.
但是,我需要价格保持在y1
,数量保持在x2
.
Which gives prices series with y1
as time and y2
as volume.
However, I need the price to remain at y1
and volume at x2
.
也许是这样的:
plot file using 1:2 with lines,' ' using 2:3 axes y1x2 with impulses
但是,这不能满足我的需求.
However, that does not give what I want.
推荐答案
Gnuplot
没有绘制这种水平箱形图的官方方法.但是,您可以使用boxxyerrorbars
(速记boxxy
)来实现此目的.
Gnuplot
has no official way to draw this kind of horizontal boxplots. However, you can use the boxxyerrorbars
(shorthand boxxy
) to achieve this.
由于我没有您的实际示例的任何测试数据,因此我从高斯随机游走中生成了一个数据文件.要生成数据,请运行以下python
脚本:
As I don't have any test data of your actual example, I generated a data file from a Gaussian random-walk. To generate the data run the following python
script:
from numpy import zeros, savetxt, random
N = 500
g = zeros(N)
for i in range(1, N):
g[i] = g[i-1] + random.normal()
savetxt('randomwalk.dat', g, delimiter='\t', fmt='%.3f')
接下来,我对位置数据"进行分箱(在您的情况下为体积数据).为此,可以使用smooth frequency
.对于相同的x
值,这将计算y
值的总和.因此,首先我使用适当的装箱功能,该功能会在一定范围(x
+-binwidth/2
)中返回相同的值.输出数据保存在文件中,因为绘图时必须交换x
和y
值:
As next thing, I do binning of the 'position data' (which in your case would be the volume data). For this one can use smooth frequency
. This computes the sum of the y
values for the same x
-values. So first I use a proper binning function, which returns the same value for a certain range (x
+- binwidth/2
). The output data is saved in a file, because for the plotting we must exchange x
and y
value:
binwidth = 2
hist(x) = floor(x+0.5)/binwidth
set output "| head -n -2 > randomwalk.hist"
set table
plot 'randomwalk.dat' using (hist($1)):(1) smooth frequency
unset table
unset output
通常一个人应该可以使用set table "randomwalk.hist"
,但是由于存在错误,一个人需要这种解决方法来过滤表输出的最后一个条目,请参阅我对.
Normally one should be able to use set table "randomwalk.hist"
, but due to a bug, one needs this workaround to filter out the last entry of the table output, see my answer to Why does the 'set table' option in Gnuplot re-write the first entry in the last line?.
现在实际的绘图部分是:
Now the actual plotting part is:
unset key
set x2tics
set xtics nomirror
set xlabel 'time step'
set ylabel 'position value'
set x2label 'frequency'
set style fill solid 1.0 border lt -1
set terminal pngcairo
set output 'randwomwalk.png'
plot 'randomwalk.hist' using ($2/2.0):($1*binwidth):($2/2.0):(binwidth/2.0) with boxxy lc rgb '#00cc00' axes x2y1,\
'randomwalk.dat' with lines lc rgb 'black'
给出结果(使用4.6.3,当然取决于您的随机数据):
which gives the result (with 4.6.3, depends of course on your random data):
因此,对于您的数据结构,以下脚本应该起作用:
So, for your data structure, the following script should work:
reset
binwidth = 2
hist(x) = floor(x+0.5)/binwidth
file = 'data.txt'
histfile = 'pricevolume.hist'
set table histfile
plot file using (hist($2)):($3) smooth unique
unset table
# get the number of records to skip the last one
stats histfile using 1 nooutput
unset key
set x2tics
set xtics nomirror
set xlabel 'time'
set ylabel 'price'
set x2label 'volume'
set style fill solid 1.0 border lt -1
plot histfile using ($2/2.0):($1*binwidth):($2/2.0):(binwidth/2.0) every ::::(STATS_records-2) with boxxy lc rgb '#00cc00' axes x2y1,\
file with lines using 1:2 lc rgb 'black'
请注意,这一次跳过最后一个table
条目是通过使用stats
命令计数所有条目,然后使用every
跳过最后一个条目来完成的(是的,STATS_records-2
是正确的,因为点编号从0
开始).此变体不需要任何外部工具.
Note, that this time the skipping of the last table
entry is done by counting all entries with the stats
command, and skipping the last one with every
(yes, STATS_records-2
is correct, because the point numbering starts at 0
). This variant doesn't need any external tool.
我还使用smooth unique
来计算的平均值,而不是求和(用smooth frequency
完成).
I also use smooth unique
, which computes the average value of the , instead of the sum (which is done with smooth frequency
).
这篇关于Gnuplot互换轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!