Python 第二个 y 轴时间序列 seaborn

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47591650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:16:29  来源:igfitidea点击:

Second y-axis time series seaborn

pythonseaborn

提问by J. Doe

Using the data frame

使用数据框

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]
})

one can plot with seabornsay column1with sns.tsplot(data=df.column1, color="g").

可以用seabornsay column1with来绘图sns.tsplot(data=df.column1, color="g")

How can we plot both time series with two y-axis in seaborn ?

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

回答by Andrey Sobolev

As seabornis built on the top of matplotlib, you can use its power:

由于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)

回答by ImportanceOfBeingErnest

I would recommend using a normal line plot. You can get a twin axes via ax.twinx().

我建议使用法线图。您可以通过 获得双轴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()

enter image description here

在此处输入图片说明