![](/img/trans.png)
[英]Use Seaborn to plot 1D time series as a line with marginal histogram along y-axis
[英]Second y-axis time series 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
說column1
和sns.tsplot(data=df.column1, color="g")
。
我們如何在 seaborn 中用兩個 y 軸繪制兩個時間序列?
由於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)
我建議使用法線圖。 您可以通過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()
您可以根據@Andrey Sobolev 的解決方案嘗試以下代碼,但這也將生成完整的圖例。
from matplotlib.lines import Line2D
g = sb.lineplot(data=df.column1, color="g")
g = sb.lineplot(data=df.column2, color="b", ax=g.axes.twinx())
g.legend([Line2D([], [], marker='_', color="g"), Line2D([], [], marker='_', color="b")], ['column 1', 'column2'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.