matplotlib / matplotlib/matplotlib

[Bug]: Axes.cla() with sharex may wrongly reset the axes limit.

未关闭
#27,825 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

topic: axes
主要语言
Python
星标
23.2k
派生
8.5k
平均合并
1 天 6 小时
30 天内合并 PR
66

描述

Bug summary

For example, when subplot A shares the x-axis with subplot B (A is created with sharex=B), a call to B.cla() will set the xlimit to [0, 1], which may not be expected (in contrast, a call to A.cla() will not change the xlimit).

I think the main problem comes from the following function, where it will check whether its _sharex attribute is None; however, in this case, B is shared to A, but no axis is shared to B, so B's _sharex attribute is None.
https://github.com/matplotlib/matplotlib/blob/9e18a343fb58a2978a8e27df03190ed21c61c343/lib/matplotlib/axes/_base.py#L1369

Code for reproduction
ax = plt.subplot(211)
ax2 = plt.subplot(212, sharex=ax)
ax.plot(range(50))
ax2.plot(range(50))
ax.cla()
plt.show()
Actual outcome

image

Expected outcome

image

Additional information

No response

Operating system

No response

Matplotlib Version

3.8.3

Matplotlib Backend

'TkAgg'

Python version

3.9.13

Jupyter version

No response

Installation

pip

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

阅读 lib/matplotlib/axes/_base.py 中链接的 Axes.cla() 代码,然后运行提供的、使用 sharex 的 subplot 复现。跟踪当 B 与 A 共享坐标轴时,共享坐标轴关系是如何表示的,并在不重置 x-limit 的情况下使行为符合预期结果。确认该复现不再意外更改 limit。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
data-visualization
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。