如何为堆叠条形图中的图例的每个标签设置唯一的颜色

时间:2020-01-22 05:19:07

标签: python-3.x pandas matplotlib

我正在处理一个大型数据集,并试图从堆积的条形图中获取一些重要信息,但是当我绘制它时,我注意到标签的颜色并不是唯一的,这对我来说是个大问题。我的目标是为我做过很多研究的每个标签设置一个独特的颜色,然后再在此处为example发布此问题。但是他们没有用,正在给出错误'AxesSubplot' object has no attribute 'set_color_cycle'

# my dataset is big so I am providing its instance
#suppose I have data of few cites and their complaints 
city = ['NEW YORK', 'ASTORIA', 'BRONX', 'BRONX', 'ELMHURST', 'BROOKLYN',
       'NEW YORK', 'BRONX', 'KEW GARDENS', 'BROOKLYN']
complaints = ['Noise - Street/Sidewalk', 'Blocked Driveway', 'Blocked Driveway',
       'Illegal Parking', 'Illegal Parking', 'Illegal Parking',
       'Illegal Parking', 'Blocked Driveway', 'Illegal Parking',
       'Blocked Driveway']
# and from this I have created a stack bar chart
test2 = new_cutmr_df[['Complaint Type', 'City']]
cmpltnt_rela = test2.groupby(['City', 'Complaint Type']).size().unstack().fillna(0).plot(kind='bar', legend = True, stacked=True)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5),ncol=2)
cmpltnt_rela.plot(figsize=(18,14))

其图看起来像这样 enter image description here

您可以在图例中看到颜色重复

0 个答案:

没有答案