matplotlib 图例前面的绘图箭头

yvt65v4c  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(146)

我是一个Python新手,我想为我的学士学位论文做一个图。这个图包括必须出现在图例中的箭头。我在Stackoverflow上看到了一些关于如何向图例添加箭头的问题,但无法遵循它们。所以我做了一条零线宽的线作为占位符,并想在图例前面生成一个箭头。不幸的是,我不能把箭头放在图例前面。你可以在倒数第二个代码行看到我的箭头。我试图用“zorder”把它移到前面,但它不起作用。它只能通过透明的图例看到,正如你可以看到在链接的图形。我很高兴为任何帮助得到这个箭头的传说。提前谢谢你!

  1. import matplotlib
  2. matplotlib.rcParams['text.usetex'] = True
  3. import numpy as np
  4. import scipy.io as sio
  5. plt.style.use('ggplot')
  6. mat=sio.loadmat('MatlabDaten/absolut.mat')
  7. x=mat['x']
  8. x=x[0]
  9. xaufp=mat['xaufp']
  10. xaufp=xaufp[0]
  11. y=mat['y']
  12. y=y[0]
  13. yaufp=mat['yaufp']
  14. yaufp=yaufp[0]
  15. pfeillaenge=mat['pfeillaenge']
  16. pfeillaenge=pfeillaenge[0]
  17. pfeilpos=mat['pfeilpos']
  18. ppfad=plt.plot(x,y,'C0',label='Geplanter Pfad')
  19. paufp=plt.plot(xaufp,yaufp,'C0o',label='Aufpunkte')
  20. plt.plot(0,0,lw=0,label='Lokale Orientierung')
  21. for count in pfeilpos:
  22. 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')
  23. plt.xlim(0,5)
  24. plt.ylim(-1.5,1.5)
  25. plt.xlabel('$x$-Koordinate')
  26. plt.ylabel('$y$-Koordinate')
  27. plt.title('Absolute Orientierung')
  28. plt.legend(handles=[ppfad,paufp,ppfeil],loc='lower left')
  29. 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')
  30. pfeil.set_zorder(+5)

结果图:x1c 0d1x

mf98qq94

mf98qq941#

处理此问题的正确方法是创建自定义图例处理程序。

  1. import matplotlib
  2. matplotlib.rcParams['text.usetex'] = True
  3. import numpy as np
  4. import scipy.io as sio
  5. from matplotlib.legend_handler import HandlerPatch
  6. import matplotlib.patches as mpatches
  7. class HandlerArrow(HandlerPatch):
  8. def create_artists(self, legend, orig_handle,
  9. xdescent, ydescent, width, height, fontsize, trans):
  10. p = mpatches.FancyArrow(0, 0.5*height, width, 0, length_includes_head=True, head_width=0.75*height )
  11. self.update_prop(p, orig_handle, legend)
  12. p.set_transform(trans)
  13. return [p]
  14. plt.style.use('ggplot')
  15. #mat=sio.loadmat('MatlabDaten/absolut.mat')
  16. #x=mat['x']
  17. #x=x[0]
  18. #xaufp=mat['xaufp']
  19. #xaufp=xaufp[0]
  20. #y=mat['y']
  21. #y=y[0]
  22. #yaufp=mat['yaufp']
  23. #yaufp=yaufp[0]
  24. #pfeillaenge=mat['pfeillaenge']
  25. #pfeillaenge=pfeillaenge[0]
  26. #pfeilpos=mat['pfeilpos']
  27. x = [1,4]
  28. y = [-1,1]
  29. xaufp = [1,4]
  30. yaufp = [-1,1]
  31. pfeilpos = [[1,-1],[4,1]]
  32. pfeillaenge = [0,0.25]
  33. fig = plt.figure()
  34. ppfad=plt.plot(x,y,'C0',label='Geplanter Pfad')
  35. paufp=plt.plot(xaufp,yaufp,'C0o',label='Aufpunkte')
  36. for count in pfeilpos:
  37. 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')
  38. plt.xlim(0,5)
  39. plt.ylim(-1.5,1.5)
  40. plt.xlabel('$x$-Koordinate')
  41. plt.ylabel('$y$-Koordinate')
  42. plt.title('Absolute Orientierung')
  43. h,l = plt.gca().get_legend_handles_labels()
  44. h.append(ppfeil)
  45. l.append('Lokale Orientierung')
  46. plt.legend(h,l, handler_map={mpatches.FancyArrow : HandlerArrow()})

请注意,这通常是矫枉过正的,您还可以使用scatter的功能来使用custom markers来欺骗matplotlib:

  1. import matplotlib
  2. matplotlib.rcParams['text.usetex'] = True
  3. import numpy as np
  4. import scipy.io as sio
  5. plt.style.use('ggplot')
  6. #mat=sio.loadmat('MatlabDaten/absolut.mat')
  7. #x=mat['x']
  8. #x=x[0]
  9. #xaufp=mat['xaufp']
  10. #xaufp=xaufp[0]
  11. #y=mat['y']
  12. #y=y[0]
  13. #yaufp=mat['yaufp']
  14. #yaufp=yaufp[0]
  15. #pfeillaenge=mat['pfeillaenge']
  16. #pfeillaenge=pfeillaenge[0]
  17. #pfeilpos=mat['pfeilpos']
  18. x = [1,4]
  19. y = [-1,1]
  20. xaufp = [1,4]
  21. yaufp = [-1,1]
  22. pfeilpos = [[1,-1],[4,1]]
  23. pfeillaenge = [0,0.25]
  24. fig = plt.figure()
  25. ppfad=plt.plot(x,y,'C0',label='Geplanter Pfad')
  26. paufp=plt.plot(xaufp,yaufp,'C0o',label='Aufpunkte')
  27. for count in pfeilpos:
  28. 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')
  29. plt.scatter([],[],marker=r'$\rightarrow$', label='Lokale Orientierung', color='C1', s=100) # dummy scatter to add an item to the legend
  30. plt.xlim(0,5)
  31. plt.ylim(-1.5,1.5)
  32. plt.xlabel('$x$-Koordinate')
  33. plt.ylabel('$y$-Koordinate')
  34. plt.title('Absolute Orientierung')
  35. plt.legend()

展开查看全部

相关问题