Thanks to visit codestin.com
Credit goes to github.com

Skip to content

axhspan() in empty plots changes the xlimits of plots sharing the X axis #18137

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

Closed
martinber opened this issue Jul 31, 2020 · 6 comments · Fixed by #18276
Closed

axhspan() in empty plots changes the xlimits of plots sharing the X axis #18137

martinber opened this issue Jul 31, 2020 · 6 comments · Fixed by #18276
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! status: confirmed bug
Milestone

Comments

@martinber
Copy link

Bug report

Consider you have a figure with multiple subplots sharing the X axis (fig.subplots(2, 2, sharex="all")). When using axhspan() in an Ax without data, the rest of Axes will change the X axis potentially leaving data outside the range.

Code for reproduction

import matplotlib.pyplot as plt

fig = plt.figure()
ax1, ax2 = fig.subplots(2, 1, sharex="all")
ax1.set_title("ax1")
ax2.set_title("ax2")

ax1.scatter([1, 2, 3, 4], [2, 3, 2, 3])
ax2.axhspan(-5, 5, alpha=0.2)

fig.show()

Actual outcome

image

Expected outcome

I expected both Axes to use a shared X axis in the range 1 to 4, because the data covers that interval

image

Matplotlib version

  • Operating system: Debian 10
  • Matplotlib version: 3.2.2
  • Matplotlib backend (print(matplotlib.get_backend())): nbAgg
  • Python version: 3.7.3
  • Jupyter version (if applicable): Happens outside of Jupyter as well

Installed via pip. Running inside a python3 -m venv

Thank you!

@tacaswell
Copy link
Member

This is another issue with making auto-scaling lazier. If you put a fig.canvas.draw() in before you call ax2.axhspan() things work correctly.

@tacaswell tacaswell added status: confirmed bug Good first issue Open a pull request against these issues if there are no active ones! Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues labels Jul 31, 2020
@tacaswell
Copy link
Member

This is a good first issue because there is no API design, I do not think it is desirable for the axhspan to completely clobber the x-scaling, but medium because it will involve walking through the auto scaling logic which is a bit convoluted due to a) trying to be lazy b) needing to touch a lot of different parts of the code.

Related to #17417

@learner296
Copy link

Hello @martinber, I am interested in this issue and want to contribute.

@martinber
Copy link
Author

Hello. I understand that this project does not "assign" issues to people, so you can try to solve it without asking for permission.

Also, I'm not part of this project, but if you need something you can ask here. I will try to help too if I can

@learner296
Copy link

Thank you @martinber. I am a novice in open source projects contribution. Please let me know how to get started? You want both interval use the same range on both x and y-axis?

@martinber
Copy link
Author

You should fork this project and start to dig into the code to find the solution, then you can make a Pull Request. Regarding this issue, I have no idea how matplotlib works or how to solve it.

In particular, I wanted both Axes to share the same x-axis range. See that the first Ax has data and that the second Ax has no data. Normally, both should have the range [1; 4], but if I use ahvspan in the Ax without data, both Axes share the interval [-0.05; 0.05]

By the way, check the pull request linked above, looks like someone already fixed this

@tacaswell tacaswell added this to the v3.4.0 milestone Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! status: confirmed bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants