【发布时间】:2017-06-13 17:22:07
【问题描述】:
这里有一个更乏味的问题,但希望有人能提供帮助。我正在尝试绘制一个汇总值(按星期几分组),但显示日期缩写并按星期几编号排序(即显示星期一,首先使用索引 0 排序)。
def Format_plot(plot):
plot.spines["top"].set_visible(False)
plot.spines["right"].set_visible(False)
plot.yaxis.set_ticks_position("left")
plot.xaxis.set_ticks_position("bottom")
#For purposes of mapping
days = {0:'Mon',1:'Tues',2:'Weds',3:'Thurs',4:'Fri',5:'Sat',6:'Sun'}
ds['signup_weekday'] = ds['signup_dayofweek'].apply(lambda x: days[x])
ds = ds.sort_values(by = 'signup_date')
first_ride_rate = {
'success': lambda x: 1.0*sum(x)/len(x)
}
def GraphMe(item):
de = ds[pd.notnull(ds[item])]
if item == 'signup_weekday':
sorter = 'signup_dayofweek'
else:
sorter = item
de = de.sort_values(by = sorter)
total_avg = 1.0*sum(de['success'])/len(de['success'])
plot = de.groupby(item).agg(first_ride_rate).plot(kind = 'bar',legend=None, title = "Share of signups to complete a first trip")
Format_plot(plot)
plot.axhline(y=total_avg, color = 'orange')
plot.set_xlabel(item)
plot.set_ylabel('Share of Signups to Complete')
print sorter
for item in ['signup_dayofweek','signup_weekday']:
GraphMe(item)
正如您所见,它不像我希望的那样在星期几索引上排序,希望有人有一个聪明的技巧。此外,如果有一种简单的方法可以将 jupyter notebook 单元格输入到堆栈溢出中,这将有助于了解更多问题。
谢谢!
【问题讨论】:
标签: python matplotlib graph jupyter-notebook