# Add plots with different ymin, ymax parameters?

I would like to combine graphs of single-variable functions with different vertical ranges specified for each.  As a test, I tried:

p=plot(x^3, x, -2, 2, ymin=-1, ymax=1)
q=plot(exp(x), x, -2, 2, ymin=0, ymax=4)
(p+q).show()


hoping to display the two graphs with the appropriate vertical ranges, but this does not give the desired result: the second ymin/ymax specification apparently overrides the first.  Various similar attempts also failed.  Is there a syntax that makes this work?

(Obviously, in this test case, one could simply change the horizontal range in each plot to produce the desired vertical range, but this would be tedious if one were adding a larger number of plots involving more complicated functions.)

edit retag close merge delete

Sort by » oldest newest most voted

Good question.

The reason for this behavior is that ymax and friends are not in fact parameters for plot, but for show; we just pass those parameters on to show(). Presumably we overwrite them - you can verify this by looking at the output of this.

sage: p+q
sage: q+p


I don't see an obvious way to fix this without rewriting the code substantially. However, this may work as a workaround for you.

sage: p1 = line([(a,b) for a,b in zip(p.xdata,p.ydata) if (b>=-1 and b<=1)])
sage: q1 = line([(a,b) for a,b in zip(q.xdata,q.ydata) if (b>=0 and b<=4)])
sage: p1+q1 We're using the fact that a Graphics object has various subobjects, and for a basic plot there is one of them which we access as the "zeroth" one, and its xdata and ydata are hopefully self-explanatory. Then we use a Python list comprehension and create a line from that data, which after all is what a plot of a function turns out to be.

Good luck!

more

Thank you for your helpful answer, which solves my problem. The fact that ymin and ymax can be used as arguments in plot() gives them the illusion of being local variables, although, as you explain, and as my example illustrates, this is not the case. Of course, the fact that ymin and ymax don't appear in the documentation of "plot" as plot options is an indication of the true situation.

True. The true situation is that anyone who ever starts overhauling one aspect of this will be tempted to rewrite the entire plot documentation, and that is naturally a big enough job that it then doesn't get done, since there is cool new functionality to implement instead. You can use parasites. This is a notebook I had on my machine. I has three plots with three different y axes.

from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

host = host_subplot(111, axes_class=AA.Axes)

par1 = host.twinx()
par2 = host.twinx()

offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
axes=par2,
offset=(offset, 0))

par2.axis["right"].toggle(all=True)

host.set_xlim(0, 2)
host.set_ylim(0, 4)

host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_ylabel("Temperature")
par2.set_ylabel("Velocity")

p1, = host.plot([0, 1, 2], [0, 1, 4], label="Density")
p2, = par1.plot([2, 4, 7], label="Temperature")
p3, = par2.plot([5, 8, 9], label="Velocity")

par1.set_ylim(0, 40)
par2.set_ylim(1, 65)

host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right"].label.set_color(p3.get_color())

plt.draw()
plt.savefig('test.png') more

Of course, this is pretty much pure matplotlib :) in which case you can do whatever you want. But after your answer I'm now not sure what the poster was asking for - concurrent plots like this, or ones like mine?

I think your solution and mine is the same. I just added additional ytics to make it clear. I am not sure whether it is against asksage etiquette to answer a sage question with a matplotlib answer. Matplotlib offers a lot of flexibility as you mention and I tend to do all my plotting in matplotlib.

No, your solution is answering a different question! And it's a good one. But the images should show what I mean by different questions. You have several different vertical axes all in the same place; mine has just one vertical axis, with the various functions.

As for the mpl issue, certainly that's a legitimate answer, it's just a lot more complicated than some users (including myself) could really implement in general. However, for this user it may be just fine, so you should definitely put it up - the more resources, the better.