我有一个 pandas 数据框,我想对其进行切片,并将每个切片绘制在单独的子图中。我想使用 sharey='all'
并让 matplotlib 决定一些合理的 y 轴限制,而不必在数据帧中搜索最小值和最大值并添加偏移量。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame(np.arange(50).reshape((5,10))).transpose()
fig, axes = plt.subplots(nrows=0,ncols=0, sharey='all', tight_layout=True)
for i in range(1, len(df.columns) + 1):
ax = fig.add_subplot(2,3,i)
iC = df.iloc[:, i-1]
iC.plot(ax=ax)
事实上,无论我指定 sharey
是什么 ('all'
,'col'
, '行'
、True
或 False
)。我使用 sharey='all'
寻求的结果如下:
有人可以解释一下我在这里做错了什么吗?
最佳答案
以下版本只会添加 df 列所需的轴并共享它们的 y 尺度:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame(np.arange(50).reshape((5,10))).transpose()
fig = plt.figure(tight_layout=True)
ref_ax = None
for i in range(len(df.columns)):
ax = fig.add_subplot(2, 3, i+1, sharey=ref_ax)
ref_ax=ax
iC = df.iloc[:, i]
iC.plot(ax=ax)
plt.show()
网格布局参数,这里明确给出为 ...add_subplot(2, 3, ...
,当然可以根据 len(df.columns )
。
关于python - plt.subplots() 中的 sharey ='all' 参数未传递给 df.plot()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50145309/