在Matplotlib+Qt轨道绘制程序中实现原点填充圆的方法问询
解决方法:用Matplotlib补丁绘制原点填充圆
嘿,我来帮你搞定这个需求!你不需要依赖pyplot,也不用纠结MatLab风格的rectangle()函数——Matplotlib的Axes对象本身就支持通过**补丁(Patch)**绘制各种形状,Circle类正好能满足你画地球填充圆的需求。
具体修改步骤:
导入Circle类:
在代码顶部的导入区加上这一行:from matplotlib.patches import Circle替换星号为填充圆:
把你原来画星号的代码self.graph.plot(0, 0, '*')删掉,换成下面的代码:# 自定义地球的半径,根据你的轨道坐标尺度调整数值 earth_radius = 0.5 # 创建圆心在(0,0)的填充圆,设置颜色和填充属性 earth_circle = Circle((0, 0), earth_radius, color='darkblue', fill=True) # 将圆添加到坐标轴上 self.graph.add_patch(earth_circle)
修改后的完整代码片段:
你的MainGUI类里的绘图部分会变成这样:
class MainGUI(QtGui.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainGUI, self).__init__(parent) self.setupUi(self) #### Lots of other code goes here #### # PlotView self.figure = Figure(tight_layout=True) self.canvas = FigureCanvas(self.figure) self.toolbar = NavigationToolbar(self.canvas, self) self.plotLayout.addWidget(self.toolbar) self.plotLayout.addWidget(self.canvas) self.graph = self.figure.add_subplot(111) self.graph.axis('equal') self.graph.plot([-3, -2, -1, 0, 1, 2, 3], [0, -1, 4, 2, 0, -3, -5]) # 绘制地球填充圆 from matplotlib.patches import Circle # 也可以移到顶部统一导入 earth_radius = 0.5 self.graph.add_patch(Circle((0, 0), earth_radius, color='darkblue', fill=True)) ### Even more code goes here ####
几个关键说明:
- 因为你已经设置了
self.graph.axis('equal'),所以绘制的圆不会被坐标轴拉伸,能保持正确的圆形比例。 - 你可以调整
Circle的参数来定制样式:比如用edgecolor设置圆的边框颜色,alpha调整透明度,linewidth修改边框粗细等。 - 之前用
rectangle()报错是因为Matplotlib里的Rectangle是画矩形的,和MatLab的语法不一样,而Circle才是专门画圆形的正确组件。
内容的提问来源于stack exchange,提问作者Kajsa
相关产品推荐
相关产品推荐

