Matplotlib-双轴共享原点,无切削数据

时间:2018-08-09 11:40:14

标签: python pandas matplotlib

我想用matplotlib在双y轴上绘制时间序列并重新缩放轴,以使两个轴的原点(y = 0)对齐。

我已经看过this问题,但是建议的解决方案可以从图中删除一些数据。

例如:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

np.random.seed(1)
df = pd.DataFrame(np.random.randn(100, 3), columns=['A', 'B', 'C']).cumsum()
df['Total'] = df[['A', 'B', 'C']].sum(1)

fig, ax = plt.subplots(1)
df.drop('Total', axis=1).plot(ax=ax)
ax2 = ax.twinx()
df['Total'].plot(ax=ax2, color='k', linestyle='--')

enter image description here

使用@drevicko解决方案(来自上面的链接),获得以下图表。现在,两个轴的原点已对齐,但绿线的一部分已留在图中。

如何对齐原点并将所有数据保留在绘图框中?

enter image description here

0 个答案:

没有答案