GR.rb icon indicating copy to clipboard operation
GR.rb copied to clipboard

Problems with `hold` method behavior

Open kojix2 opened this issue 4 years ago • 0 comments

Recent changes have caused the axis position to change depending on whether a label or title is present. This sometimes caused the hold to not work correctly. This problem is related to #22 . In julia, the state seems to be maintained by the global variables ctx and plt.

case1

  • Use the hash returned by hold as is.
  • The line plot drawn later deviates from y=0.
require 'numo/narray'
require 'gr/plot'
DFloat = Numo::DFloat
NMath = Numo::NMath

x = DFloat.linspace(0, 10, 500)
y = NMath.sin(x**2) * NMath.exp(-x)
kw = {title: "Example plot", xlabel: "X", ylabel: "Y", update: false}

GR.beginprint("img.png") do
  GR.plot(x, y, kw.clone)
  k = GR.hold
  k.merge!({update: false})
  GR.plot(x, -2 * NMath.exp(x)**-1, k)
  GR.plot(x, 2 * NMath.exp(x)**-1, k)
  GR.updatews
end

img

case2

  • Add title and label to the hash returned by hold.
  • The line plot drawn later will be displayed correctly.
require 'numo/narray'
require 'gr/plot'
DFloat = Numo::DFloat
NMath = Numo::NMath

x = DFloat.linspace(0, 10, 500)
y = NMath.sin(x**2) * NMath.exp(-x)
kw = {title: "Example plot", xlabel: "X", ylabel: "Y", update: false}

GR.beginprint("img.png") do
  GR.plot(x, y, kw.clone) # should be fiexed
  k = GR.hold
  k.merge!(kw)
  GR.plot(x, -2 * NMath.exp(x)**-1, k)
  GR.plot(x, 2 * NMath.exp(x)**-1, k)
  GR.updatews
end

img

kojix2 avatar Aug 03 '21 10:08 kojix2