Python 在matplotlib图中绘制填充圆并用Qt显示
我正在写一个程序来绘制环绕地球的轨道。轨道由x和y坐标列表表示,并使用Python 在matplotlib图中绘制填充圆并用Qt显示,python,qt,matplotlib,Python,Qt,Matplotlib,我正在写一个程序来绘制环绕地球的轨道。轨道由x和y坐标列表表示,并使用plot()函数绘制 现在我想让地球显示为一个围绕原点具有正确半径的填充圆。我尝试使用MatLab函数rectangle(),但收到错误消息,表示没有吸吮属性 我在其他资料中读到,使用pyplot可能会导致意外行为,我希望在不使用该库的情况下实现这一点 简而言之,我想要一个半径为r的圆,在这个图中*的周围 如何在MWE中实现此功能: import sys from matplotlib.backends.backend_q
plot()
函数绘制
现在我想让地球显示为一个围绕原点具有正确半径的填充圆。我尝试使用MatLab函数rectangle()
,但收到错误消息,表示没有吸吮属性
我在其他资料中读到,使用pyplot
可能会导致意外行为,我希望在不使用该库的情况下实现这一点
简而言之,我想要一个半径为r的圆,在这个图中*的周围
如何在MWE中实现此功能:
import sys
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from PyQt4 import QtCore, QtGui, uic
qtCreatorMain = "GUI.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorMain)
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])
self.graph.plot(0, 0, '*')
### Even more code goes here ####
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainGUI()
window.show()
sys.exit(app.exec_())
GUI.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string><html><head/><body><p align="center"><span style=" font-size:20pt; font-weight:600;">Lots of stuff goes</span></p><p align="center"><span style=" font-size:20pt; font-weight:600;">in this part</span></p></body></html></string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="plotLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
</layout>
</item>
</layout>
<zorder>verticalLayoutWidget</zorder>
<zorder>frame</zorder>
<zorder>label</zorder>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>27</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
主窗口
0
0
800
600
主窗口
QFrame::StyledPanel
QFrame::提出
htmlhead/bodyp align=“center”span style=“font size:20pt;font-weight:600;”这部分有很多东西/span/pp align=“center”span style=“font-size:20pt;font-weight:600;”/span/p/body/html
0
QLayout::SetDefaultConstraint
垂直向外延伸
框架
标签
0
0
800
27
当然,您可以使用matplotlib.pyplot轻松创建圆:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
circle = plt.Circle((0,0), radius)
ax.add_artist(circle)
如果我们检查圆圈的类型
:
print (type(circle))
# <class 'matplotlib.patches.Circle'>
其中:
MatLab函数或matplotlib???@DavidG,我在另一个问题中读到,由于一些奇怪的行为,您不应该导入
pyplot
,而应该使用figure
。没有其他方法吗?@eyllanesc,因为我假设matplotlib将具有与matlab相同的函数,所以我尝试使用从那里知道的函数,但没有成功。
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])
self.graph.plot(0, 0, '*')
self.circle = matplotlib.patches.Circle((0,0), 0.5, color="r")
self.graph.add_artist(self.circle)
### Even more code goes here ####