如何为图例的单个值设置唯一的颜色 [英] How to set unique color for individual value of legend

查看:156
本文介绍了如何为图例的单个值设置唯一的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在堆积的条形图中有许多图例,我注意到在图例中颜色是重复的,因此我很难根据图例区分图形中的真实值,因此,我想为每个图样设置唯一的颜色为此,我做了很多研究,有些没有用,有些很难理解示例

I have many legends in my stacked bar plot and I noticed that in legend the color is repeating so it's hard for me to distinguish the true value in the graph according to the legends so, I want to set the unique color for each value in the legend and for this, I did lots of research some are not working and some are quite hard to understand example this when I used this I got an error that 'AxesSubplot' object has no attribute 'set_color_cycle' so is there an easy and effective way

我不希望代码单独为每个元素应用颜色,因为我的数据集很大,在这里我的代码可提供有关绘图的更多详细信息

I don't want the code that applies color for each element individually because my dataset is large and here my code for more detail about my plot

例如

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

及其结果看起来像这样,您可以注意到图例的元素颜色

and its result looks something like this where you can notice legend's element color

推荐答案

您可以创建与唯一投诉数量相同长度的颜色列表.例如gist_ncar.在代码中,我重新排列了颜色的顺序,以使相似的颜色接近的可能性降低.

You might create a list of colors with the same length as the number of unique complaints. For example gist_ncar. In the code I shuffled the order of the colors to make it less likely that similar colors are near.

请注意,很难拥有超过20种视觉上足够不同的颜色.不同的人和不同的显示器可能会导致颜色难以区分.

Note that it is very hard to have more than 20 colors that are different enough visually. Different people and different monitors may cause colors hard to distinguish.

帖子提供了更多的想法来选择足够的颜色.在您的情况下,用类似的颜色为类似的投诉涂上颜色可能会很有趣.

This and this post provide more ideas to choose enough colors. In your case it might be interesting to color similar complaints with similar hues.

由于您的示例代码无法提供足够的数据,因此下面的代码会生成一些随机数.

As your example code doesn't provide enough data, the code below generates some random numbers.

import pandas as pd
from matplotlib import pyplot as plt
import random
import matplotlib as mpl

city = ['Londen', 'Paris', 'Rome', 'Brussels', 'Cologne', 'Madrid',
        'Athens', 'Geneva', 'Oslo', 'Barcelona', 'Berlin']
complaints = list('abcdefghijklmnopqrstuv')

N = 100
city_column = random.choices(city, k=N)
complaints_column = random.choices(complaints, k=N)
test2 = pd.DataFrame({'City': city_column, 'Complaint Type': complaints_column})

# take a colormap with many different colors and sample it at regular intervals
cmap = plt.cm.gist_rainbow
norm = mpl.colors.Normalize(vmin=0, vmax=len(complaints) - 1)
colors = [cmap(norm(i)) for i in range(len(complaints))]

# create a stack bar chart
cmpltnt_rela = test2.groupby(['City', 'Complaint Type']).size().unstack().fillna(0).plot(kind='bar',
      legend=True, stacked=True, color=colors)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), ncol=2)
cmpltnt_rela.plot(figsize=(18, 14))
plt.tick_params('x', labelrotation=30)

plt.tight_layout()
plt.show()

这篇关于如何为图例的单个值设置唯一的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆