【问题标题】:Gnuplot interchanging AxesGnuplot 交换轴
【发布时间】:2013-09-19 12:34:16
【问题描述】:

我想用 gnuplot 重现这个情节:

我的数据格式如下:

数据
1:时间
2:价格
3:音量

我试过这个:

plot file using 1:2 with lines, '' using 1:3 axes x1y2 with impulses

这给出了一个正常的时间序列图表,其中y1 作为价格,y2 作为交易量。
接下来,我尝试了:

plot file using 2:1 with lines, '' using 2:3 axes x1y2 with impulses 

它给出了以y1 为时间和y2 为数量的价格系列。 但是,我需要将价格保持在y1,将成交量保持在x2

可能是这样的:

plot file using 1:2 with lines,' ' using 2:3 axes y1x2 with impulses

但是,这并没有给我想要的。

【问题讨论】:

  • plot 'file' using 2:1 做你想做的事吗?否则,请提供示例输入文件和有关您想要的更多详细信息。
  • 是的,它在第二个命令中

标签: gnuplot


【解决方案1】:

Gnuplot 没有官方的方法来绘制这种水平箱线图。但是,您可以使用boxxyerrorbars(简写boxxy)来实现这一点。

由于我没有您的实际示例的任何测试数据,因此我从高斯随机游走生成了一个数据文件。要生成数据,请运行以下python 脚本:

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)。输出数据保存在一个文件中,因为要进行绘图,我们必须交换 xy 值:

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",但由于一个错误,需要这种解决方法来过滤掉表输出的最后一个条目,请参阅我对Why does the 'set table' option in Gnuplot re-write the first entry in the last line?的回答。

现在实际的绘图部分是:

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,当然取决于你的随机数据):

因此,对于您的数据结构,以下脚本应该可以工作:

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 开始)。此变体不需要任何外部工具。

我还使用 smooth unique,它计算 的平均值,而不是总和(使用 smooth frequency 完成)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多