我有一个数据框如下:
![enter image description here](https://i.stack.imgur.com/jD8BR.png)
在上面的数据框中,'Month'
是一个有序的Categorical
列定义为:
cats = ['January', 'February', 'March', 'April','May','June', 'July', 'August','September', 'October', 'November', 'December']
month_gr['Month'] = pd.Categorical(month_gr['Month'], cats, ordered = True)
使用 Seaborn 条形图:
ax = sns.barplot(data = month_gr, x = 'Item Name', y = 'Total', hue = 'Month')
ax.set_xticklabels(ax.get_xticklabels(), rotation= 90, ha = 'right')
Outputs:
![enter image description here](https://i.stack.imgur.com/RKmN0.png)
上面的图例显示了 12 个月的全部Categorical
柱子。我只想显示图例 4 个月['June', 'July', 'August', 'September']
,因为我的数据只包含这 4 个月。有没有办法动态控制图例,使其仅显示可用的Categories
传递给data
?