在matplotlib图中绘制实心圆并用Qt显示

时间:2018-01-15 15:25:35

标签: python qt matplotlib

我正在编写一个程序来绘制绕地球运行的轨道。轨道由x和y坐标列表表示,并用plot()函数绘制。

现在我想让地球显示为一个圆形,原点周围的半径正确。我尝试使用MatLab函数rectangle(),但收到错误消息,说没有suck属性。

我已在其他来源中读到,使用pyplot可能会导致意外行为,并希望在不使用该库的情况下实现此目的。

简而言之,我希望在此图中围绕 * 的半径 r 填充圆圈。

enter image description here

如何在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>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-size:20pt; font-weight:600;&quot;&gt;Lots of stuff goes&lt;/span&gt;&lt;/p&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-size:20pt; font-weight:600;&quot;&gt;in this part&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</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>

1 个答案:

答案 0 :(得分:1)

您当然可以使用matplotlib.pyplot轻松创建一个圆圈:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
circle = plt.Circle((0,0), radius)
ax.add_artist(circle)

如果我们检查circle的类型:

print (type(circle))
# <class 'matplotlib.patches.Circle'>

因此,如果您想在不导入pyplot的情况下执行此操作,只需使用matplotlib.patches.Circle

即可

对于您的具体示例,您的__init__类似于:

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 ####

给出了:

enter image description here