Как установить одинаковые цвета для одинаковых индексов на разных графиках в matplotlib и seaborn

import operator, itertools

def is_monotone(lst):
    op = operator.le            # pick 'op' based upon trend between
    if not op(lst[0], lst[-1]): # first and last element in the 'lst'
        op = operator.ge
    return all(op(x,y) for x, y in itertools.izip(lst, lst[1:]))
3
задан Neo 19 January 2019 в 16:31
поделиться

1 ответ

Вам нужно будет создать словарь, который будет искать соответствующий цвет для значения метки, которую вы используете. Ниже приведена упрощенная версия, которая, надеюсь, охватывает все, что вы делаете с вашими данными:

# a dataframe from a list of lists 
el = [['RED', 50, 1], 
      ['GREEN', 30, 2], 
      ['BLUE', 7, 3], 
      ['YELLOW', 3, 4], 
      ['ORANGE', 9, 5], 
      ['BLACK', 1, 6]]
df = pd.DataFrame(el)

# since you are not using the entire dataframe
df_thres = df[df[1] > 1]

# make a fixed length color map manually
c = ['red', 'green', 'blue', 'yellow', 'orange']
clist1 = {i:j for i, j in zip(df_thres[0].values, c)}

# make an arbitrary-length colormap
cm = plt.get_cmap('rainbow')
c = [cm(1.0 * i/len(df_thres)) for i in range(len(df_thres))]
clist2 = {i:j for i, j in zip(df_thres[0].values, c)}


fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(11, 8))

# manual colors

# PIE PLOT: sort on the third column
newdf = df_thres.sort_values(2, ascending=False)
ax[0, 0].pie(newdf[2], labels=newdf[0], colors=[clist1[i] for i in newdf[0].values])
ax[0, 0].axis('square') # matplotlib 2.x only

# BAR PLOT: sort on the second column    
newdf = df_thres.sort_values(1, ascending=False)
ax[0, 1].bar(newdf[0], newdf[1], color=[clist1[i] for i in newdf[0].values])

#----------------

# arbitrary-length generated colorlist
newdf = df_thres.sort_values(2, ascending=False)
ax[1, 0].pie(newdf[1], labels=newdf[0], colors=[clist2[i] for i in newdf[0].values])
ax[1, 0].axis('square') # matplotlib 2.x only

newdf = df_thres.sort_values(1, ascending=False)
ax[1, 1].bar(newdf[0], newdf[1], color=[clist2[i] for i in newdf[0].values])

Это вывод, который я получаю. Цвета в нижнем ряду не помечены, но соответствуют друг другу. (Также обратите внимание, что я использую matplotlib для обоих графиков здесь)

enter image description here

0
ответ дан Neo 19 January 2019 в 16:31
поделиться
Другие вопросы по тегам:

Похожие вопросы: