【问题标题】:Custom legend of several lines with two markers for the same text具有相同文本的两个标记的多行自定义图例
【发布时间】:2019-01-06 22:37:08
【问题描述】:

我想执行图中 中显示的图例类型。

我用一些技巧做的那个图只是为了代表我真正想要的东西,所以它不能很好地显示图例中粉红色标记的颜色。

图中的圆圈代表相同的参数,但用于两个不同的模型;三角形和方形标记也是如此。我想将引用相同参数的两个圆形标记放置在图例的第一行中,类似地,将其他两个标记放在该行下方的行中。谢谢。

代码:

import matplotlib.pyplot as plt

q1 = [100.0, 60.0, 200.0, 300.0]
NO1 = [0.35799999999999998, 0.33100000000000002, 0.22900000000000001,     0.17799999999999999]
No1 = [0.34599999999999997, 0.29899999999999999, 0.20699999999999999, 0.14999999999999999]
Nb1 = [0.46600000000000003, 0.45600000000000002, 0.27800000000000002, 0.24399999999999999]

q2 = [60.0, 100.0, 200.0, 300.0]
NO2 = [0.44700000000000001, 0.29199999999999998, 0.28299999999999997, 0.253]
No2 = [0.38900000000000001, 0.28499999999999998, 0.311, 0.251]
Nb2 = [0.44, 0.34899999999999998, 0.45900000000000002, 0.39400000000000002]

fig, ax = plt.subplots(figsize = (6,3))

ax.plot(q1, NO1, marker = 'o', markerfacecolor = 'none', markeredgewidth = 1, color = 'gray', linestyle = '', markersize = 8, label = '$N$ in parameter a')
ax.plot(q2, NO2, marker = 'o', markerfacecolor = 'none', markeredgewidth = 1, color = 'palevioletred', linestyle = '', markersize = 8)#, label = '$N$ in parameter a')
ax.plot(q1, No1, marker = '^', markerfacecolor = 'none', markeredgewidth = 1, color = 'gray', linestyle = '', markersize = 8, label = '$N$ in parameter b')
ax.plot(q2, No2, marker = '^', markerfacecolor = 'none', markeredgewidth = 1, color = 'palevioletred', linestyle = '', markersize = 8)#, label = '$N$ in parameter b')  
ax.plot(q1, Nb1, marker = 's', markerfacecolor = 'none', markeredgewidth = 1, color = 'gray', linestyle = '', markersize = 8, label = '$N$ in parameter c') 
ax.plot(q2, Nb2, marker = 's', markerfacecolor = 'none', markeredgewidth = 1, color = 'palevioletred', linestyle = '', markersize = 8)#, label = '$N$ in parameter c')
#plt.legend(loc='upper right', bbox_to_anchor=(0.945, 1))
plt.xlabel('x')
plt.ylabel('$N$')
plt.xticks([60, 100, 200, 300])
plt.minorticks_on()
plt.tick_params(direction = 'in', bottom = True, top = True, left = True, right = True, which = 'major')    
plt.tick_params(direction = 'in', bottom = False, top = False, left = True, right = True, which = 'minor')  

【问题讨论】:

  • 当然可以,但是您能否提供一个测试用例(即可以使用的代码)。是否只涉及两种不同的颜色?
  • @ImportanceOfBeingErnest 是的,只有两个。

标签: python matplotlib legend


【解决方案1】:

您可以使用HandlerTuple 处理程序并提供艺术家的元组以在每一行中显示为图例的句柄。

import matplotlib.pyplot as plt
import matplotlib.legend_handler

q1 = [100.0, 60.0, 200.0, 300.0]
NO1 = [0.358, 0.331, 0.229, 0.178]
No1 = [0.346, 0.299, 0.207, 0.15]
Nb1 = [0.466, 0.456, 0.278, 0.244]

q2 = [60.0, 100.0, 200.0, 300.0]
NO2 = [0.447, 0.292, 0.283, 0.253]
No2 = [0.389, 0.285, 0.311, 0.251]
Nb2 = [0.44, 0.349, 0.459, 0.394]

fig, ax = plt.subplots(figsize = (6,3))

prop = dict(markerfacecolor = 'none', markeredgewidth = 1,
            linestyle = '', markersize = 8,)
l1, = ax.plot(q1, NO1, marker = 'o', color = 'gray', label = '$N$ in parameter a', **prop)
l2, = ax.plot(q2, NO2, marker = 'o', color = 'palevioletred', **prop)
l3, = ax.plot(q1, No1, marker = '^', color = 'gray', label = '$N$ in parameter b', **prop)
l4, = ax.plot(q2, No2, marker = '^', color = 'palevioletred', **prop)
l5, = ax.plot(q1, Nb1, marker = 's', color = 'gray', label = '$N$ in parameter c', **prop) 
l6, = ax.plot(q2, Nb2, marker = 's', color = 'palevioletred', **prop)


handles = [(l1,l2), (l3,l4), (l5,l6)]
_, labels = ax.get_legend_handles_labels()

ax.legend(handles = handles, labels=labels, loc='upper right', 
          handler_map = {tuple: matplotlib.legend_handler.HandlerTuple(None)})

plt.xlabel('x')
plt.ylabel('$N$')

plt.show()

【讨论】:

  • 感谢您的回复。昨天我认为你的代码有效。你做了什么改变吗?今天编译时出现这个错误。 “ handler_map = {tuple: matplotlib.legend_handler.HandlerTuple(None)}) TypeError: __init__() 接受 1 个位置参数,但给出了 2 个 “你能告诉我错误是什么吗?
  • 我没有更改任何内容(您会看到答案是否与您在 16 小时前编辑问题的方式相似)。您是否在两个不同版本的 matplotlib 或 python 中运行它?使用HandlerTuple(ndivide=None)时是否有效?
  • 它也给出了错误:“ HandlerBase.__init__(self, **kwargs) TypeError: __init__() got an unexpected keyword argument 'ndivide'”
  • import matplotlib; print(matplotlib.__version__) 给你什么?
  • 它给出:2.0.0
猜你喜欢
  • 2017-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
  • 2016-09-20
  • 2016-09-21
相关资源
最近更新 更多