感谢@swatchai。我修改了他的答案以适合我的代码。所以我得到了:
import numpy as np
import matplotlib.lines as mlines
import matplotlib.pyplot as plt
# my fig and my axes
fig, ax1 = plt.subplots(figsize=(6, 6))
# my plots
ax1.plot(temp, zn, color=cycle[0], label=r'$z_n$')
ax1.plot(temp, zp, color=cycle[1], label=r'$z_p$')
ax1.plot(temp, zpi, color=cycle[2], label=r'$z_\pi$')
# the other plots
ax1.plot(temp, zn2, color=cycle[0], linestyle='--')
ax1.plot(temp, zp2, color=cycle[1], linestyle='--')
ax1.plot(temp, zpi2, color=cycle[2], linestyle='--')
# Second legend 'imaginary' lines
line_solid = mlines.Line2D([], [], color='black', linestyle='-', \
linewidth=1.5, label=r'$n_b = n_0$')
line_dashed = mlines.Line2D([], [], color='black', linestyle='--', \
linewidth=1.5, label=r'$n_b = n_0/2$')
# original legend
leg1 = ax1.legend()
# set second legend (will remove first one)
leg2 = ax1.legend(handles=[line_solid, line_dashed], loc='best', \
bbox_to_anchor=(0.5, 0.20, 0.5, 0.6))
leg2.set_frame_on(False) # remove legend frame
# manually add the first legend back
ax1.add_artist(leg1)
输出(注意上面的代码是不可运行的,而且我似乎还不能嵌入图片):
result
我实际上是想避免必须通过创建新的假想线来为它们分配图例这一步骤。我很想知道是否也可以在文本中使用标记。不过好吧,这至少解决了我的问题。
谢谢!