【问题标题】:I want two legends for same graph. In the example given, I want two legends for red and blue color bars我想要同一张图的两个图例。在给出的示例中,我想要红色和蓝色颜色条的两个图例
【发布时间】:2021-06-13 05:16:35
【问题描述】:
`import matplotlib.pyplot as plt
 x_axis=['blah','blahs','yeah']
 y_axis=[10,20,30]
 col=['blue' if i<=10 else'red' for i in y_axis]
 plt.bar(x_axis,y_axis,color=col]`

【问题讨论】:

  • 您希望将图形作为点的线或标记?
  • 我想要条形图

标签: python matplotlib legend


【解决方案1】:

您必须在 legend 函数示例中创建自定义,例如

import matplotlib.pyplot as plt
x_axis=['blah','blahs','yeah']
y_axis=[10,20,30]

col=['blue' if i<=10 else'red' for i in y_axis]

plt.bar(x_axis,y_axis,color=col)

colors = {'less than 10 group':'blue', 'greater than 10 group':'red'}         
labels = list(colors.keys())
handles = [plt.Rectangle((0,0),1,2, color=colors[label]) for label in labels] # it is legend design like rectangle

plt.legend(handles, labels) 
plt.show()

docs

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2017-04-19
    • 2020-11-10
    • 2016-12-29
    • 2012-12-15
    • 2016-06-26
    • 2013-09-17
    • 2019-01-04
    • 2021-02-22
    相关资源
    最近更新 更多