Seaborn barplot with regression line

那年仲夏 提交于 2019-12-01 07:32:44

问题


Is there a way to add a regression line to a barplot in seaborn where the x axis contains pandas.Timestamps?

For example, overlay a trendline in this bar plot below. Am looking for the most efficient way to do this:

seaborn.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 = seaborn.barplot(data=a, x=a.index, y=a.Attendees, color='lightblue', )
seaborn.despine(offset=10, trim=False)
ax.set_ylabel("")
ax.set_xticklabels(['Dec', 'Jan','Feb','Mar','Apr'])
plt.show()


回答1:


Seaborn barplots are categorical plots. Categorical plots cannot directly be used for regression, because the numeric values would not fit. The usual matplotlib bar plots however use numeric data.

An option is to plot a matplotlib barplot and seaborn regplot in the same graph.

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()

Since seaborn's barplot uses the integers from 0 to number of bars as indizes, one can also use those indizes for a regression plot on top of seaborn bar plot.

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/44357379/seaborn-barplot-with-regression-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!