Seaborn barplots是絕對的地塊。分類圖不能直接用於迴歸,因爲數值不適合。然而,通常的matplotlib條形圖卻使用數字數據。
一個選項是在同一個圖中繪製matplotlib barplot和seaborn regplot。
import numpy as np; np.random.seed(1)
import seaborn.apionly as sns
import matplotlib.pyplot as plt
x = np.linspace(5,9,13)
y = np.cumsum(np.random.rand(len(x)))
fig, ax = plt.subplots()
ax.bar(x,y, width=0.1, color="lightblue", zorder=0)
sns.regplot(x=x, y=y, ax=ax)
ax.set_ylim(0, None)
plt.show()
由於seaborn的barplot使用從0整數爲indizes的條數,還可以使用那些indizes對seaborn條形圖頂部的迴歸圖。
import numpy as np
import seaborn.apionly as sns
import matplotlib.pyplot as plt
import pandas
sns.set(style="white", context="talk")
a = pandas.DataFrame.from_dict({'Attendees': {pandas.Timestamp('2016-12-01'): 10,
pandas.Timestamp('2017-01-01'): 12,
pandas.Timestamp('2017-02-01'): 15,
pandas.Timestamp('2017-03-01'): 16,
pandas.Timestamp('2017-04-01'): 20}})
ax = sns.barplot(data=a, x=a.index, y=a.Attendees, color='lightblue')
# put bars in background:
for c in ax.patches:
c.set_zorder(0)
# plot regplot with numbers 0,..,len(a) as x value
sns.regplot(x=np.arange(0,len(a)), y=a.Attendees, ax=ax)
sns.despine(offset=10, trim=False)
ax.set_ylabel("")
ax.set_xticklabels(['Dec', 'Jan','Feb','Mar','Apr'])
plt.show()
相關,但沒有確切的重複:https://stackoverflow.com/questions/40558128/using-datetimes-with-seaborns-regplot – ImportanceOfBeingErnest