How can one freeze the axis range in ruby-gnuplot, and overplot on top of the frozen plot?

StackOverflow https://stackoverflow.com/questions/23333718

  •  10-07-2023
  •  | 
  •  

Question

I would like to plot a generic dataset

plot.data << ::Gnuplot::DataSet.new([x, y]) do |ds|
  ds.with = "lines linewidth 2"
  ds.notitle
end

and then freeze the automatic y-range of the plot in order to plot a vertical line at some position 'x1', from the bottom to the top of this y-range. I set up some variables and call plot:

a = [x1, x1]
b = [0, y.sort.last * 1.1]

plot.data << ::Gnuplot::DataSet.new([a, b]) do |ds|
  ds.with = "lines linewidth 2 lt 0 lc 3"
  ds.notitle
end

where the values of b were chosen to fill the y axis range. But, of course, gnuplot expands the axis range to accommodate our large value of the array b. So rather than having a line drawn from top to bottom, I still have a line that extends about 90% of the way up the plot but my data is squished down a little bit.

Was it helpful?

Solution

You can draw a vertical line which spans the whole graph using set arrow with graph coordinates. Taking the sin_wave.rb example, here is an example:

$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
require "gnuplot"
Gnuplot.open do |gp|
  Gnuplot::Plot.new( gp ) do |plot|

    plot.xrange "[-10:10]"
    plot.title  "Sin Wave Example"
    plot.ylabel "sin(x)"
    plot.xlabel "x"

    x1 = 2
    plot.arrow "from first %f,graph 0 rto first 0,graph 1 nohead lw 2 lt 0 lc 3" % x1

    plot.data << Gnuplot::DataSet.new( "sin(x)" ) do |ds|
      ds.with = "lines"
      ds.linewidth = 4
    end

  end
  sleep 10
end

with the result

enter image description here

For available coordinate systems other than first and graph, see e.g. https://stackoverflow.com/a/23180595/2604213

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top