1

I am writing a program to plot orbits around the Earth. The orbits are represented by lists of x and y coordinates and plotted with the plot() function.

Now I would like to have Earth shown as a filled circle with the correct radius around origin. I tried to use the MatLab function rectangle() but get error messages saying that there is no suck attribute.

I have read in other sources that using pyplot might cause unexpected behavior and would like to achieve this without using that library.

In short, I would like a filled circle with radius r around the * in this figure.

enter image description here

How can I implement this function in my 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>
Kajsa
  • 211
  • 1
  • 16
  • MatLab function or matplotlib?? – eyllanesc Jan 15 '18 at 15:31
  • @DavidG, I read in another question that you shouldn't import `pyplot` but use `figure` instead due to some strange behavior. Is there no other way? – Kajsa Jan 15 '18 at 16:08
  • @eyllanesc, As I assumed matplotlib would have the same functions as matlab I tried using a function I knew from there, without success. – Kajsa Jan 15 '18 at 16:10

1 Answers1

1

You can of course use matplotlib.pyplot to create a circle quite easily:

import matplotlib.pyplot as plt

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

If we examine the type of circle:

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

Therefore, if you wanted to do this without importing pyplot, simply use matplotlib.patches.Circle

For your specific example, your __init__ would look something like:

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

Which gives:

enter image description here

DavidG
  • 24,279
  • 14
  • 89
  • 82