我的问题源于提供的解决方案
here
.
在我下面的代码中,我想
自动地
以变量名列表为例,
十、
,并为颜色图中的每个变量指定一种颜色(例如,使用get_cmap)。我也只希望每个变量在图例中出现一次。在本例中,变量B&H被复制,我分别给他们分配了limegreen和black。
import matplotlib.pyplot as plt
x = ["A","B","B","C","D","E","H","F","G","H"]
y = [-25, -10, -5, 5, 10, 30, 35, 40, 50, 60]
w = [30, 20, 30, 25, 40, 20, 40, 40, 40, 30]
colors = ["yellow","limegreen","limegreen","green","blue","red","black","brown","grey","black"]
plt.figure(figsize=(20,10))
xticks=[]
for n, c in enumerate(w):
xticks.append(sum(w[:n]) + w[n]/2)
w_new = [i/max(w) for i in w]
a = plt.bar(xticks, height = y, width = w, color = colors, alpha = 0.8)
_ = plt.xticks(xticks, w)
plt.legend(a.patches, x)