1

I would like to reproduce this plot with gnuplot:

enter image description here

My data has this format:

Data
1: time
2: price
3: volume

I tried this:

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

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 

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.

Maybe something like:

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

However, that does not give what I want.

2
  • Does plot 'file' using 2:1 do what you want? Otherwise, provide sample input file and more details about what you want.
    – Bernhard
    Sep 19, 2013 at 12:36
  • Yes and it does in the 2nd command Sep 19, 2013 at 15:21

1 Answer 1

3

Gnuplot has no official way to draw this kind of horizontal boxplots. However, you can use the boxxyerrorbars (shorthand boxxy) to achieve this.

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')

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

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'

which gives the result (with 4.6.3, depends of course on your random data):

enter image description here

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'

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.

I also use smooth unique, which computes the average value of the , instead of the sum (which is done with smooth frequency).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.