如何使用颜色图并使用 matplotlib 为堆积条形图上的特定值设置特定颜色
How to use a color map and have a specific color for a certain value on a stacked bar using matplotlib
我正在创建一个堆积条形图来显示不同 'events' 在不同 'positions' 处的频率 [每个位置都是一个条形,它被那里发生的事件分开]。我希望每个事件都具有独特的颜色并且可以区分,所以我想使用彩虹色图。不过最后的活动我想是白色。
到目前为止,我只有使用彩虹的情节代码,最后一个事件为红色。我怎样才能让它变白?
import pandas as pd
import matplotlib as plt
from matplotlib import cm
import numpy as np
r = [0,1,2,3]
raw_data = {'1': [20, 5, 3, 2], '2': [5, 15, 5, 4],'3': [3, 5, 13, 17], '4': [2, 3, 7, 5], '5':[5, 7, 7, 7]}
df = pd.DataFrame(raw_data)
event_colours = plt.cm.rainbow(np.linspace(0, 1, 5))
sbc = df.plot.bar(stacked = True, color = event_colours)
sbc.set(xlabel="Position", ylabel="Frequency")
sbc.legend(loc='center right', bbox_to_anchor=(-0.2, 0.5))
一种解决方案是通过使用 patches
访问最上面的条来简单地更改它们的颜色。然后,更新相应的图例条目。另一种可能的解决方案是创建自定义颜色图。
由于白色背景上的白色条不可见,我选择了 lightgrey
颜色来演示我的解决方案。
# Your 4 import commands here
import matplotlib.patches as mpatches
# Your code goes here
# ............
# Change the color of the last 4 bars
for patch in list(sbc.axes.patches)[-4:]:
patch.set_facecolor('lightgrey')
# Update the legend entry
hans, labs = sbc.get_legend_handles_labels() # Get the existing legends
last_bar = mpatches.Patch(color='lightgrey', label='5')
del labs[-1], hans[-1] # Delete the last red legend entry
hans.append(last_bar) # add the grey bar to the legend
sbc.legend(handles=hans, loc=2, bbox_to_anchor=(-0.3, 0.7))
将最后一种颜色设置为白色,例如通过 event_colours[-1] = (1,1,1,1)
.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
r = [0,1,2,3]
raw_data = {'1': [20, 5, 3, 2], '2': [5, 15, 5, 4],'3': [3, 5, 13, 17],
'4': [2, 3, 7, 5], '5':[5, 7, 7, 7]}
df = pd.DataFrame(raw_data)
event_colours = plt.cm.rainbow(np.linspace(0, 1, 5))
event_colours[-1] = (1,1,1,1)
ax = df.plot.bar(stacked = True, color = event_colours, edgecolor="k")
ax.set(xlabel="Position", ylabel="Frequency")
ax.legend(loc='center right', bbox_to_anchor=(-0.2, 0.5))
ax.figure.subplots_adjust(left=0.3)
plt.show()
我正在创建一个堆积条形图来显示不同 'events' 在不同 'positions' 处的频率 [每个位置都是一个条形,它被那里发生的事件分开]。我希望每个事件都具有独特的颜色并且可以区分,所以我想使用彩虹色图。不过最后的活动我想是白色。
到目前为止,我只有使用彩虹的情节代码,最后一个事件为红色。我怎样才能让它变白?
import pandas as pd
import matplotlib as plt
from matplotlib import cm
import numpy as np
r = [0,1,2,3]
raw_data = {'1': [20, 5, 3, 2], '2': [5, 15, 5, 4],'3': [3, 5, 13, 17], '4': [2, 3, 7, 5], '5':[5, 7, 7, 7]}
df = pd.DataFrame(raw_data)
event_colours = plt.cm.rainbow(np.linspace(0, 1, 5))
sbc = df.plot.bar(stacked = True, color = event_colours)
sbc.set(xlabel="Position", ylabel="Frequency")
sbc.legend(loc='center right', bbox_to_anchor=(-0.2, 0.5))
一种解决方案是通过使用 patches
访问最上面的条来简单地更改它们的颜色。然后,更新相应的图例条目。另一种可能的解决方案是创建自定义颜色图。
由于白色背景上的白色条不可见,我选择了 lightgrey
颜色来演示我的解决方案。
# Your 4 import commands here
import matplotlib.patches as mpatches
# Your code goes here
# ............
# Change the color of the last 4 bars
for patch in list(sbc.axes.patches)[-4:]:
patch.set_facecolor('lightgrey')
# Update the legend entry
hans, labs = sbc.get_legend_handles_labels() # Get the existing legends
last_bar = mpatches.Patch(color='lightgrey', label='5')
del labs[-1], hans[-1] # Delete the last red legend entry
hans.append(last_bar) # add the grey bar to the legend
sbc.legend(handles=hans, loc=2, bbox_to_anchor=(-0.3, 0.7))
将最后一种颜色设置为白色,例如通过 event_colours[-1] = (1,1,1,1)
.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
r = [0,1,2,3]
raw_data = {'1': [20, 5, 3, 2], '2': [5, 15, 5, 4],'3': [3, 5, 13, 17],
'4': [2, 3, 7, 5], '5':[5, 7, 7, 7]}
df = pd.DataFrame(raw_data)
event_colours = plt.cm.rainbow(np.linspace(0, 1, 5))
event_colours[-1] = (1,1,1,1)
ax = df.plot.bar(stacked = True, color = event_colours, edgecolor="k")
ax.set(xlabel="Position", ylabel="Frequency")
ax.legend(loc='center right', bbox_to_anchor=(-0.2, 0.5))
ax.figure.subplots_adjust(left=0.3)
plt.show()