Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

colorbar might shrink plots if used with twinx #8823

Closed
PrometheusPi opened this issue Jun 29, 2017 · 5 comments

Comments

Projects
None yet
5 participants
@PrometheusPi
Copy link

commented Jun 29, 2017

Bug report

Changing plot alignment for different placing of colorbar() in twinx plot

If one combines a pcolormesh plot/hist2d plot with a standard plot by using twinx, the graph from the plot moves to wrong x values if the colorbar is called after the plot call. If it is called before, the alignment is fine.

I am not sure if this is a bug or just wrong usage.
However, one of the resulting graphs has a wrong extent that might lead to misinterpretations.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

data = np.random.random((64, 128)) * 2.0
x = np.arange(128)
y = np.arange(64)
plt.pcolormesh(x, y, data)

#plt.colorbar() --> here it works fine
plt.twinx()
plt.colorbar()
# --> here it gives wrong x values for the orange plot

plt.plot(x, np.sin(x/10), color="orange", lw=4)

plt.show()

Actual outcome

misaligned_twinx_wrong

Next to the confusing layout the plot shows actually a wrong graph:
the orange plot just goes to around x=100 but should go to x=128.
(The axis of the colorbar is correct and shows the range of data.)

Expected outcome

misaligned_twinx_correct

If colorbar is called before twinx, the orange graph has the correct extent.

I am not sure if this is a bug or a wrong usage of matplotlib. In any case the wrong extent of the (in this case) orange plot is a dangerous behavior that might lead to misinterpretations of the data.

Matplotlib version

  • Operating System: Ubuntu 14.04.1
  • Matplotlib Version: 2.0.0rc1
  • Python Version: 3.4.3
  • Jupyter Version: 1.0.0 (I am using %matplotlib inline)
  • Other Libraries: numpy 1.12.0b1

I installed matplotlib via pip.

@efiring

This comment has been minimized.

Copy link
Member

commented Jun 29, 2017

I would call it a shortcoming of the twinx implementation: it makes a second Axes at the same position as the first, but it has no mechanism for ensuring that if the position of either is subsequently changed, the position of the other is also changed to match it. Ideally, the position of both Axes would be the same object so that it would not be necessary to use callbacks to keep them synchronized. There are several attributes related to position, so I'm not sure whether this would be a simple change, a difficult one, or a complete can of worms.

@tacaswell tacaswell added this to the 2.2 (next next feature release) milestone Jun 30, 2017

@jklymak

This comment has been minimized.

Copy link
Contributor

commented Dec 11, 2017

#9082 addresses this (though you can't use the pyplot interface)

data = np.random.random((64, 128)) * 2.0
x = np.arange(128)
y = np.arange(64)
fig, ax = plt.subplots(constrained_layout=True)
pcm = ax.pcolormesh(x, y, data)

#plt.colorbar() --> here it works fine
ax2 = ax.twinx()
fig.colorbar(pcm)
# --> here it gives wrong x values for the orange plot

ax2.plot(x, np.sin(x/10), color="orange", lw=4)

plt.show()

figure_1

@PrometheusPi

This comment has been minimized.

Copy link
Author

commented Dec 12, 2017

@jklymak Thanks for posting this. I will cherry pick your pull request.

@efiring

This comment has been minimized.

Copy link
Member

commented Jan 21, 2018

This is also addressed by #10033.

@efiring

This comment has been minimized.

Copy link
Member

commented Jan 26, 2018

Closed by #10033.

@efiring efiring closed this Jan 26, 2018

@QuLogic QuLogic modified the milestones: needs sorting, v2.2.0 Feb 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.