matplotlib 为什么颜色会从colormap中随机删除?

nvbavucw  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(155)

这是我的代码:

import pandas as pd

data = {'bar_groups': ['A', 'A', 'B', 'C', 'B', 'A', 'C', 'B', 'C', 'B'],
        'color_groups': [1, 2, 3, 4, 5, 6, 7, 8, 1, 2],
        0: [0.258, 0.087, 0.168, 0.241, 0.182, 0.236, 0.231, 0.092, 0.283, 0.186],
        1: [0.212, 0.208, 0.135, 0.24, 0.256, 0.27, 0.218, 0.151, 0.162, 0.18],
        2: [0.009, 0.062, 0.031, 0.017, 0.027, 0.02, 0.043, 0.087, 0.011, 0.04],
        3: [0.006, 0.015, 0.006, 0.009, 0.009, 0.01, 0.016, 0.006, 0.004, 0.011],
        4: [0.002, 0.002, 0.002, 0.002, 0.007, 0.005, 0.18, 0.002, 0.025, 0.004],
        5: [0.268, 0.269, 0.262, 0.278, 0.278, 0.269, 0.19, 0.229, 0.395, 0.234],
        6: [0.004, 0.017, 0.008, 0.009, 0.018, 0.002, 0.005, 0.012, 0.002, 0.04],
        7: [0.242, 0.338, 0.387, 0.204, 0.222, 0.188, 0.117, 0.422, 0.117, 0.306],
        8: [0.006, 0.015, 0.006, 0.009, 0.009, 0.01, 0.016, 0.006, 0.004, 0.011]}
df = pd.DataFrame(data)
    
          
# Plot code

fig, ax = plt.subplots(figsize=(10,7))
# bars on the left 
df_target = df.groupby(["bar_groups", "color_groups"]).size()
df_target = (df_target / df_target.groupby("bar_groups").transform(sum)).unstack(level='color_groups')
df_target.plot(kind='bar',stacked=True, position=1, colormap='Set3', width=0.4, ax=ax, edgecolor='grey')

# bars on the right
df_prob = df[['bar_groups', 0,1,2,3,4,5,6,7,8]].groupby('bar_groups').mean()
df_prob.plot.bar(stacked=True, ax=ax, position=0, cmap='Set3', width=0.4, legend=False, alpha=0.6, edgecolor='grey')
ax.set_xlim(-0.75,9)
ax.set_ylabel('Actual (left) & Predicted (right) bars')
plt.title('Grouped bar chart')

由于外部原因,我不得不删除最后一列(8),现在颜色完全改变了- matplotlib似乎删除了序列中的随机颜色(粉红色,紫色)。

kpbwa7wx

kpbwa7wx1#

df_target

color_groups         1         2     3         4     5         6         7     8
bar_groups                                                                      
A             0.333333  0.333333   NaN       NaN   NaN  0.333333       NaN   NaN
B                  NaN  0.250000  0.25       NaN  0.25       NaN       NaN  0.25
C             0.333333       NaN   NaN  0.333333   NaN       NaN  0.333333   NaN

df_prob

0         1         2         3        4         5         6        7         8
bar_groups                                                                                        
A           0.193667  0.230000  0.030333  0.010333  0.00300  0.268667  0.007667  0.25600  0.010333
B           0.157000  0.180500  0.046250  0.008000  0.00375  0.250750  0.019500  0.33425  0.008000
C           0.251667  0.206667  0.023667  0.009667  0.06900  0.287667  0.005333  0.14600  0.009667
  • Dataframe 没有相同的列,因此cmap不会将相同的颜色应用于两个绘图调用。
  • 列名必须Map到特定的颜色,然后传递给color参数。
  • 这些是'Set3'的颜色

import matplotlib as mpl
import pandas as pd
import matplotlib.pyplot as plt

# create a dict where each column name is mapped to a specific color
color = dict(zip(range(9), mpl.colormaps['Set3'](range(9))))

fig, ax = plt.subplots(figsize=(10, 7))
# bars on the left 
df_target = df.groupby(["bar_groups", "color_groups"]).size()
df_target = (df_target / df_target.groupby("bar_groups").transform(sum)).unstack(level='color_groups')
df_target.plot(kind='bar',stacked=True, position=1, color=color, width=0.4,  ax=ax, edgecolor='grey')

# bars on the right
df_prob = df[['bar_groups', 0,1,2,3,4,5,6,7,8]].groupby('bar_groups').mean()
df_prob.plot(kind='bar', stacked=True, ax=ax, position=0, color=color, width=0.4, rot=0, legend=True, alpha=0.6, edgecolor='grey')
ax.set_xlim(-0.5, 2.5)
ax.set(title='Grouped bar chart', ylabel='Actual (left) & Predicted (right) bars', xlabel='Bar Groups')

ax.legend(title='Color Groups', bbox_to_anchor=(1, 0.5), loc='center left', frameon=False)
  • 两个图都显示图例,请参见给定列名的颜色相同。

cmap='Set3'绘图

  • 在图例中可以看到,给定列名的颜色不相同。

相关问题