Guest User

Untitled

a guest
Jan 17th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # make some data
  5. x = np.linspace(0, 2*np.pi)
  6. y1 = np.sin(x)
  7. y2 = np.cos(x)
  8.  
  9. # plot sin(x) and cos(x)
  10. p1 = plt.plot(x, y1, 'b-', linewidth=1.0)
  11. p2 = plt.plot(x, y2, 'r-', linewidth=1.0)
  12.  
  13. # make a legend for both plots
  14. leg = plt.legend([p1, p2], ['sin(x)', 'cos(x)'], loc=1)
  15.  
  16. # set the linewidth of each legend object
  17. for legobj in leg.legendHandles:
  18. legobj.set_linewidth(2.0)
  19.  
  20. plt.show()
  21.  
  22. import matplotlib.pyplot as plt
  23. from matplotlib import legend_handler
  24. from matplotlib.lines import Line2D
  25. import numpy as np
  26.  
  27. class MyHandlerLine2D(legend_handler.HandlerLine2D):
  28. def create_artists(self, legend, orig_handle,
  29. xdescent, ydescent, width, height, fontsize,
  30. trans):
  31.  
  32. xdata, xdata_marker = self.get_xdata(legend, xdescent, ydescent,
  33. width, height, fontsize)
  34.  
  35. ydata = ((height-ydescent)/2.)*np.ones(xdata.shape, float)
  36. legline = Line2D(xdata, ydata)
  37.  
  38. self.update_prop(legline, orig_handle, legend)
  39. #legline.update_from(orig_handle)
  40. #legend._set_artist_props(legline) # after update
  41. #legline.set_clip_box(None)
  42. #legline.set_clip_path(None)
  43. legline.set_drawstyle('default')
  44. legline.set_marker("")
  45. legline.set_linewidth(10)
  46.  
  47.  
  48. legline_marker = Line2D(xdata_marker, ydata[:len(xdata_marker)])
  49. self.update_prop(legline_marker, orig_handle, legend)
  50. #legline_marker.update_from(orig_handle)
  51. #legend._set_artist_props(legline_marker)
  52. #legline_marker.set_clip_box(None)
  53. #legline_marker.set_clip_path(None)
  54. legline_marker.set_linestyle('None')
  55. if legend.markerscale != 1:
  56. newsz = legline_marker.get_markersize()*legend.markerscale
  57. legline_marker.set_markersize(newsz)
  58. # we don't want to add this to the return list because
  59. # the texts and handles are assumed to be in one-to-one
  60. # correpondence.
  61. legline._legmarker = legline_marker
  62.  
  63. return [legline, legline_marker]
  64.  
  65.  
  66. plt.plot( [0, 1], [0, 1], '-r', lw=1, label='Line' )
  67. plt.legend(handler_map={Line2D:MyHandlerLine2D()})
  68.  
  69. plt.show()
  70.  
  71. import numpy as np
  72. import matplotlib.pyplot as plt
  73.  
  74. # make some data
  75. x = np.linspace(0, 2*np.pi)
  76. y1 = np.sin(x)
  77. y2 = np.cos(x)
  78.  
  79. fig, ax = plt.subplots()
  80. ax.plot(x, y1, linewidth=1.0, label='sin(x)')
  81. ax.plot(x, y2, linewidth=1.0, label='cos(x)')
  82. leg = ax.legend()
  83.  
  84. for line in leg.get_lines():
  85. line.set_linewidth(4.0)
  86.  
  87. plt.show()
Add Comment
Please, Sign In to add comment