第二个y轴时间序列seaborn

新手上路,请多包涵

使用数据框

df = pd.DataFrame({
    "date" : ["2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04"],
    "column1" : [555,525,532,585],
    "column2" : [50,48,49,51]
})

可以用 seaborn 来绘制 column1sns.tsplot(data=df.column1, color="g")

我们如何在 seaborn 中用两个 y 轴绘制两个时间序列?

原文由 J. Doe 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 958
2 个回答

我建议使用法线图。您可以通过 ax.twinx() 获得双轴。

 import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"date": ["2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04"],
                   "column1": [555,525,532,585],
                   "column2": [50,48,49,51]})

ax = df.plot(x="date", y="column1", legend=False)
ax2 = ax.twinx()
df.plot(x="date", y="column2", ax=ax2, legend=False, color="r")
ax.figure.legend()
plt.show()

在此处输入图像描述

原文由 ImportanceOfBeingErnest 发布,翻译遵循 CC BY-SA 3.0 许可协议

由于 seaborn 建立在 matplotlib 之上,您可以使用它的强大功能:

 import matplotlib.pyplot as plt
sns.lineplot(data=df.column1, color="g")
ax2 = plt.twinx()
sns.lineplot(data=df.column2, color="b", ax=ax2)

原文由 Andrey Sobolev 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题