我是一个Python新手,我想为我的学士学位论文做一个图。这个图包括必须出现在图例中的箭头。我在Stackoverflow上看到了一些关于如何向图例添加箭头的问题,但无法遵循它们。所以我做了一条零线宽的线作为占位符,并想在图例前面生成一个箭头。不幸的是,我不能把箭头放在图例前面。你可以在倒数第二个代码行看到我的箭头。我试图用“zorder”把它移到前面,但它不起作用。它只能通过透明的图例看到,正如你可以看到在链接的图形。我很高兴为任何帮助得到这个箭头的传说。提前谢谢你!
import matplotlib
matplotlib.rcParams['text.usetex'] = True
import numpy as np
import scipy.io as sio
plt.style.use('ggplot')
mat=sio.loadmat('MatlabDaten/absolut.mat')
x=mat['x']
x=x[0]
xaufp=mat['xaufp']
xaufp=xaufp[0]
y=mat['y']
y=y[0]
yaufp=mat['yaufp']
yaufp=yaufp[0]
pfeillaenge=mat['pfeillaenge']
pfeillaenge=pfeillaenge[0]
pfeilpos=mat['pfeilpos']
ppfad=plt.plot(x,y,'C0',label='Geplanter Pfad')
paufp=plt.plot(xaufp,yaufp,'C0o',label='Aufpunkte')
plt.plot(0,0,lw=0,label='Lokale Orientierung')
for count in pfeilpos:
ppfeil=plt.arrow(count[0],count[1],pfeillaenge[0],pfeillaenge[1], fc='C1', ec='C1', lw = 0.5, head_width=0.05, head_length=0.06, overhang = 0.3, length_includes_head= True, clip_on = False, label='Lokale Orientierung')
plt.xlim(0,5)
plt.ylim(-1.5,1.5)
plt.xlabel('$x$-Koordinate')
plt.ylabel('$y$-Koordinate')
plt.title('Absolute Orientierung')
plt.legend(handles=[ppfad,paufp,ppfeil],loc='lower left')
pfeil=plt.arrow(0.15,-1.32,0.3,0, fc='C1', ec='C1', lw = 0.5, head_width=0.05, head_length=0.06, overhang = 0.3, length_includes_head= True, clip_on = False, label='Lokale Orientierung')
pfeil.set_zorder(+5)
结果图:x1c 0d1x
1条答案
按热度按时间mf98qq941#
处理此问题的正确方法是创建自定义图例处理程序。
请注意,这通常是矫枉过正的,您还可以使用
scatter
的功能来使用custom markers来欺骗matplotlib: