import matplotlib.pyplot as plt
fruit = ['apple', 'blueberry', 'cherry', 'orange']
fruitColor = ['red', 'blue', 'red', 'orange']
count = [40, 100, 30, 50]
color = ['red', 'blue', 'red', 'orange']
plt.bar(fruit,count, color=fruitColor, label=color)
plt.ylabel('fruit supply')
plt.title('Fruit supply by kind and color')
plt.legend(title='Fruit color')
plt.show()
可以发现,图例中red出现了两次,但是我们想让它只出现一次
import matplotlib.pyplot as plt
fruit = ['apple', 'blueberry', 'cherry', 'orange']
fruitColor = ['red', 'blue', 'red', 'orange']
count = [40, 100, 30, 50]
color = ['red', 'blue', '_red', 'orange']
plt.bar(fruit,count, color=fruitColor, label=color)
plt.ylabel('fruit supply')
plt.title('Fruit supply by kind and color')
plt.legend(title='Fruit color')
plt.show()
我们修改了列表color中的red,改为了_red
标签:plt,supply,color,标签,fruit,图例,orange,legend,red From: https://blog.csdn.net/2302_80063517/article/details/140287685