排序不适用于我的热图

Posted

技术标签:

【中文标题】排序不适用于我的热图【英文标题】:Sorting does not work for my heatmap 【发布时间】:2018-02-05 13:36:02 【问题描述】:

我有以下 DataFrame df_imd(给出了一小段摘录):

id  weekday daily_avg_count
A13 7       37.000000
A13 1       46.470588
A13 6       46.812500
B14 7       46.875000
C15 4       48.294118

运行以下代码后,我无法弄清楚为什么工作日没有在热图中排序:

norm = plt.Normalize(df_imd["daily_avg_count"].values.min(), df_imd["daily_avg_count"].values.max())
colors = plt.cm.YlGnBu(norm(df_imd["daily_avg_count"]))

df_imd.loc[df_imd['weekday'] == "1", 'weekday'] = "Monday"
df_imd.loc[df_imd['weekday'] == "2", 'weekday'] = "Tuesday"
df_imd.loc[df_imd['weekday'] == "3", 'weekday'] = "Wednesday"
df_imd.loc[df_imd['weekday'] == "4", 'weekday'] = "Thursday"
df_imd.loc[df_imd['weekday'] == "5", 'weekday'] = "Friday"
df_imd.loc[df_imd['weekday'] == "6", 'weekday'] = "Saturday"
df_imd.loc[df_imd['weekday'] == "7", 'weekday'] = "Sunday"

sorter = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
df_imd.weekday = df_imd.weekday.astype("category")
df_imd.weekday.cat.set_categories(sorter, inplace=True)

iterables = [df_imd['id'].unique(),df_imd['weekday'].unique()]
df = df_imd.set_index(['id','weekday'])
df = df.reindex(index=pd.MultiIndex.from_product(iterables, names=['id', 'weekday']), fill_value=0).reset_index()

df = df.pivot("weekday", "id", "daily_avg_count")

plt.figure(figsize=(12,8))
ax = sns.heatmap(df, annot=False, linewidths=.5, cmap="YlGnBu")
plt.show()

我看到的不是“星期一”、“星期二”等,而是“星期三”、“星期一”等。

【问题讨论】:

【参考方案1】:

你需要改变:

df_imd.weekday = df_imd.weekday.astype("category")
df_imd.weekday.cat.set_categories(sorter, inplace=True)

到:

df_imd.weekday = df_imd.weekday.astype("category", ordered=True, categories=sorter)

对于有序的分类。

我尝试更多改进解决方案:

df_imd.loc[df_imd['weekday'] == "1", 'weekday'] = "Monday"
df_imd.loc[df_imd['weekday'] == "2", 'weekday'] = "Tuesday"
df_imd.loc[df_imd['weekday'] == "3", 'weekday'] = "Wednesday"
df_imd.loc[df_imd['weekday'] == "4", 'weekday'] = "Thursday"
df_imd.loc[df_imd['weekday'] == "5", 'weekday'] = "Friday"
df_imd.loc[df_imd['weekday'] == "6", 'weekday'] = "Saturday"
df_imd.loc[df_imd['weekday'] == "7", 'weekday'] = "Sunday"

sorter = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
df_imd.weekday = df_imd.weekday.astype("category")
df_imd.weekday.cat.set_categories(sorter, inplace=True)

到:

sorter = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
#create dict for maping weekdays
d = dict(zip(np.arange(1,len(sorter)+1).astype(str), sorter))
print (d)
'7': 'Sunday', '1': 'Monday', '6': 'Saturday', 
'5': 'Friday', '2': 'Tuesday', '3': 'Wednesday', '4': 'Thursday'

df_imd['weekday'] = df_imd['weekday'].map(d)
df_imd.weekday = df_imd.weekday.astype("category", ordered=True, categories=sorter)

【讨论】:

您能否解释一下为什么我的解决方案不起作用?谢谢。 在我看来,您的代码使用默认分类,它们没有排序。并且未排序的分类按字母顺序排序,因此尽管您使用 set_categories 对它们进行排序,但仍处于最终状态。

以上是关于排序不适用于我的热图的主要内容,如果未能解决你的问题,请参考以下文章

为啥排序不适用于矢量?

数据表搜索和排序不适用于动态添加的数据

冒泡排序不适用于对 C++ 中的动态对象数组进行排序

jQuery.repeater 不适用于可排序的 jQuery Ui

禁用列排序不适用于多个 angularjs 数据表

Angular:Mat Sort 不适用于类似扩展的表格