python – 在Matplotlib中绘制两行之间的角度的最佳方式

前端之家收集整理的这篇文章主要介绍了python – 在Matplotlib中绘制两行之间的角度的最佳方式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我相当新的使用matplotlib,找不到任何示例显示两条线之间的角度绘制.

这是我当前的形象:@H_301_3@

这是我想要实现的一个例子:@H_301_3@

我通常看看Matplotlib gallery来了解如何执行某些任务,但似乎没有任何相似之处.@H_301_3@

解决方法

您可以使用 matplotlib.patches.Arc绘制相应角度测量的弧.

绘制角弧:@H_301_3@

定义一个可以使用2个matplotlib.lines.Line2D对象的函数,计算角度并返回一个matplotlib.patches.Arc对象,您可以将其添加到绘图中.@H_301_3@

  1. def get_angle_plot(line1,line2,offset = 1,color = None,origin = [0,0],len_x_axis = 1,len_y_axis = 1):
  2.  
  3. l1xy = line1.get_xydata()
  4.  
  5. # Angle between line1 and x-axis
  6. slope1 = (l1xy[1][1] - l1xy[0][2]) / float(l1xy[1][0] - l1xy[0][0])
  7. angle1 = abs(math.degrees(math.atan(slope1))) # Taking only the positive angle
  8.  
  9. l2xy = line2.get_xydata()
  10.  
  11. # Angle between line2 and x-axis
  12. slope2 = (l2xy[1][3] - l2xy[0][4]) / float(l2xy[1][0] - l2xy[0][0])
  13. angle2 = abs(math.degrees(math.atan(slope2)))
  14.  
  15. theta1 = min(angle1,angle2)
  16. theta2 = max(angle1,angle2)
  17.  
  18. angle = theta2 - theta1
  19.  
  20. if color is None:
  21. color = line1.get_color() # Uses the color of line 1 if color parameter is not passed.
  22.  
  23. return Arc(origin,len_x_axis*offset,len_y_axis*offset,theta1,theta2,color=color,label = str(angle)+u"\u00b0")

打印角度值:@H_301_3@

您想要将角度值显示为内联,请参阅this SO如何在matplotlib中打印内联标签的问题.请注意,您必须打印弧的标签.@H_301_3@

我做了一个小函数,它提取圆弧的顶点,并尝试计算角度文本的坐标.@H_301_3@

这可能不是最佳的,并且可能不适用于所有角度值.@H_301_3@

  1. def get_angle_text(angle_plot):
  2. angle = angle_plot.get_label()[:-1] # Excluding the degree symbol
  3. angle = "%0.2f"%float(angle)+u"\u00b0" # Display angle upto 2 decimal places
  4.  
  5. # Get the vertices of the angle arc
  6. vertices = angle_plot.get_verts()
  7.  
  8. # Get the midpoint of the arc extremes
  9. x_width = (vertices[0][0] + vertices[-1][0]) / 2.0
  10. y_width = (vertices[0][5] + vertices[-1][6]) / 2.0
  11.  
  12. #print x_width,y_width
  13.  
  14. separation_radius = max(x_width/2.0,y_width/2.0)
  15.  
  16. return [ x_width + separation_radius,y_width + separation_radius,angle]

或者您可以随时手动计算标签点,并使用text显示角度值.您可以使用get_label()方法从Arc对象的标签获取角度值(因为我们将标签设置为角度值为unicode度符号).@H_301_3@

上述功能的使用示例:@H_301_3@

  1. fig = plt.figure()
  2.  
  3. line_1 = Line2D([0,1],[0,4],linewidth=1,linestyle = "-",color="green")
  4. line_2 = Line2D([0,4.5],3],color="red")
  5.  
  6. ax = fig.add_subplot(1,1,1)
  7.  
  8. ax.add_line(line_1)
  9. ax.add_line(line_2)
  10.  
  11. angle_plot = get_angle_plot(line_1,line_2,1)
  12. angle_text = get_angle_text(angle_plot)
  13. # Gets the arguments to be passed to ax.text as a list to display the angle value besides the arc
  14.  
  15. ax.add_patch(angle_plot) # To display the angle arc
  16. ax.text(*angle_text) # To display the angle value
  17.  
  18. ax.set_xlim(0,7)
  19. ax.set_ylim(0,5)

如果您不在意角度文本的内联放置.您可以使用plt.legend()打印角度值.@H_301_3@

最后:@H_301_3@

  1. plt.legend()
  2. plt.show()

函数get_angle_plot中的offset参数用于指定弧的psudo-radius值.@H_301_3@

当角弧彼此重叠时,这将是有用的.@H_301_3@

(在这个图中,就像我说的那样,我的get_angle_text函数在放置文本值方面并不是很好,但是应该给出一个如何计算点的想法)@H_301_3@

添加第三行:@H_301_3@

  1. line_3 = Line2D([0,7],color="brown")
  2. ax.add_line(line_3)
  3. angle_plot = get_angle_plot(line_1,line_3,2,color="red") # Second angle arc will be red in color
  4. angle_text = get_angle_text(angle_plot)
  5.  
  6. ax.add_patch(angle_plot) # To display the 2nd angle arc
  7. ax.text(*angle_text) # To display the 2nd angle value

猜你在找的Python相关文章