Easy mistake to make...

Just got bit by this and I thought I’d share to help others.

I was just quickly writing out some pyplot commands to create two subplots to compare some results. I did:

plt.subplots(1, 2, 1)
plt.contourf(…)

plt.title(“Contours”)
xlim = plt.xlim()
ylim = plt.ylim()

plt.subplots(1, 2, 2)
plt.imshow(…)
plt.title(“Raw Image”)
plt.xlim(xlim)
plt.ylim(ylim)

Did you see the error? I did “subplots” instead of “subplot”. Since the third argument for plt.subplot is “sharex”, a value of 1 or 2 appears perfectly valid to it. Meanwhile, the second call to plt.subplots() throws out my first subplot, and I also get the seemingly odd behavior of the first subplot having the correct x limits, but the default y limits (0, 1). Of course, this makes sense once you figure out the issue, but it is an extra wrinkle that can be quite confusing.

I suspect this is a very easy mistake to make. Should we perhaps test the value of sharex in subplots() and warn if it is anything but a python bool? Just a thought.

Cheers!
Ben Root

+1 : I switch subplots and subplot quite frequently, so a check would be helpful.

-Tony

···

On Thu, May 24, 2012 at 9:54 AM, Benjamin Root <ben.root@…1304…> wrote:

Just got bit by this and I thought I’d share to help others.

I was just quickly writing out some pyplot commands to create two subplots to compare some results. I did:

plt.subplots(1, 2, 1)
plt.contourf(…)

plt.title(“Contours”)
xlim = plt.xlim()
ylim = plt.ylim()

plt.subplots(1, 2, 2)
plt.imshow(…)
plt.title(“Raw Image”)
plt.xlim(xlim)
plt.ylim(ylim)

Did you see the error? I did “subplots” instead of “subplot”. Since the third argument for plt.subplot is “sharex”, a value of 1 or 2 appears perfectly valid to it. Meanwhile, the second call to plt.subplots() throws out my first subplot, and I also get the seemingly odd behavior of the first subplot having the correct x limits, but the default y limits (0, 1). Of course, this makes sense once you figure out the issue, but it is an extra wrinkle that can be quite confusing.

I suspect this is a very easy mistake to make. Should we perhaps test the value of sharex in subplots() and warn if it is anything but a python bool? Just a thought.

Cheers!
Ben Root

We could also do a check in the reverse case… if plt.subplot(1, 2, True) is done, that should either raise an error or at least warn (currently, it treats True as the first subplot and False as some (probably non-existant) subplot).

I will write up a PR for this.

Ben Root

···

On Thu, May 24, 2012 at 10:09 AM, Tony Yu <tsyu80@…287…> wrote:

On Thu, May 24, 2012 at 9:54 AM, Benjamin Root <ben.root@…1304…> wrote:

Just got bit by this and I thought I’d share to help others.

I was just quickly writing out some pyplot commands to create two subplots to compare some results. I did:

plt.subplots(1, 2, 1)
plt.contourf(…)

plt.title(“Contours”)
xlim = plt.xlim()
ylim = plt.ylim()

plt.subplots(1, 2, 2)
plt.imshow(…)
plt.title(“Raw Image”)
plt.xlim(xlim)
plt.ylim(ylim)

Did you see the error? I did “subplots” instead of “subplot”. Since the third argument for plt.subplot is “sharex”, a value of 1 or 2 appears perfectly valid to it. Meanwhile, the second call to plt.subplots() throws out my first subplot, and I also get the seemingly odd behavior of the first subplot having the correct x limits, but the default y limits (0, 1). Of course, this makes sense once you figure out the issue, but it is an extra wrinkle that can be quite confusing.

I suspect this is a very easy mistake to make. Should we perhaps test the value of sharex in subplots() and warn if it is anything but a python bool? Just a thought.

Cheers!
Ben Root

+1 : I switch subplots and subplot quite frequently, so a check would be helpful.

-Tony

Seems like a good idea. Maybe we gracefully deprecate this? i.e.
warn about the confusing usage now, and throw exceptions in a future
version?

Mike
···

http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/Matplotlib-users@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/matplotlib-users