pandas中df.plot(sharex=True)的功能疑问及行为解析
我在理解pandas绘图中的sharex参数时遇到了困难。我可以在matplotlib中实现轴共享,但尝试使用df.plot()的sharex参数时,其表现与预期不符。
以下是一个简单示例(使用Series,DataFrame情况相同):
import numpy as np import pandas as pd import matplotlib.pyplot as plt t = np.arange(0, 20, 0.01) s = pd.Series(np.sin(t), index=t) ax1 = plt.subplot(211) ax2 = plt.subplot(212) s.plot(ax=ax1, sharex=True) s.plot(ax=ax2, sharex=True) plt.xlim(0, 6) plt.show()
我观察到的唯一效果是顶部图表隐藏了x轴标签。我至少期望两张图表的x轴范围都被限制在[0,6](目前只有底部图表生效),同时期望实现同步缩放。
根据官方文档描述:
sharex : bool, default True if ax is None else False
In casesubplots=True, share x axis and set some x axis labels to invisible; defaults to True if ax is None otherwise False if an ax is passed in; Be aware, that passing in both an ax andsharex=Truewill alter all x axis labels for all axis in a figure.
我忽略了什么?这种行为是否符合预期?“共享x轴”到底指什么?我希望理解其设计逻辑,不需要解决方案。
我并非唯一对此感到困惑的用户。
内容的提问来源于stack exchange,提问作者Michel de Ruiter
相关产品推荐
相关产品推荐

