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

Closed
@martinber

Description

@martinber

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesGood first issueOpen a pull request against these issues if there are no active ones!status: confirmed bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions