簡體   English   中英

Matplotlib 和 PyQt5 繪圖圖

[英]Matplotlib and PyQt5 Plotting Figure

嘗試 plot 餅圖與 PyQt5 中的 python 和 Z1587383CF8C21507D06FB51EF7B755DDZ 有一些問題。 我可以有一個餅圖和我想在其中顯示的數據,但是如果我在調用某個 function 時提供新數據,我會嘗試更新圖表。

mplvl 是我要添加圖形的小部件名稱。

每次我調用這個 function 它只是增加了另一個子圖。 我試過清除 plot,清除圖形,但我不能讓它只刪除並重繪圖形。 我想我對 Matplotlib 如何真正起作用感到困惑,但我們將不勝感激。

import datetime
import calendar as cal
import sys

from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox, QTableWidgetItem
from PyQt5.uic import loadUi

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (FigureCanvasQTAgg as FigureCanvas)


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        loadUi('main.ui', self)

        self.date_from.setDate(QtCore.QDate(c_date.year, c_date.month, 1))
        self.date_to.setDate(QtCore.QDate(c_date.year, c_date.month, cal.monthrange(c_date.year, c_date.month)[1]))

        self.date_from.dateChanged.connect(self.fcn_date_changed)
        self.date_to.dateChanged.connect(self.fcn_date_changed)


    def fcn_date_changed(self):
        print("from: " + self.date_from.text() + " to: " + self.date_to.text())

        labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
        sizes = [15, 30, 45, 10]

        fig = Figure()
        ax1 = fig.add_subplot(111)
        ax1.set_title('Percentage Spending by Category')
        ax1.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=False, startangle=90)
        main.canvas = FigureCanvas(fig)
        main.mplvl.addWidget(main.canvas)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

下面是main.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>861</width>
    <height>611</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="btn_add_item">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>10</y>
      <width>71</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>Add Item</string>
    </property>
   </widget>
   <widget class="QPushButton" name="btn_edit_item">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>540</y>
      <width>81</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Edit Item</string>
    </property>
   </widget>
   <widget class="QListWidget" name="recent_items">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>130</y>
      <width>351</width>
      <height>401</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="lbl_recent_items">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>110</y>
      <width>251</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>Recent Items</string>
    </property>
   </widget>
   <widget class="QPushButton" name="btn_delete_item">
    <property name="geometry">
     <rect>
      <x>280</x>
      <y>540</y>
      <width>81</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Delete Item</string>
    </property>
   </widget>
   <widget class="QWidget" name="mplwindow" native="true">
    <property name="geometry">
     <rect>
      <x>380</x>
      <y>130</y>
      <width>471</width>
      <height>401</height>
     </rect>
    </property>
    <property name="sizePolicy">
     <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
      <horstretch>0</horstretch>
      <verstretch>0</verstretch>
     </sizepolicy>
    </property>
    <layout class="QVBoxLayout" name="mplvl"/>
   </widget>
   <widget class="QPushButton" name="btn_view_items">
    <property name="geometry">
     <rect>
      <x>280</x>
      <y>100</y>
      <width>81</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>View Items</string>
    </property>
   </widget>
   <widget class="QDateEdit" name="date_from">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>60</y>
      <width>110</width>
      <height>21</height>
     </rect>
    </property>
    <property name="calendarPopup">
     <bool>true</bool>
    </property>
   </widget>
   <widget class="QDateEdit" name="date_to">
    <property name="geometry">
     <rect>
      <x>250</x>
      <y>60</y>
      <width>110</width>
      <height>21</height>
     </rect>
    </property>
    <property name="calendarPopup">
     <bool>true</bool>
    </property>
   </widget>
   <widget class="QLabel" name="lbl_showing_items1">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>60</y>
      <width>101</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>Showing items from: </string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl_showing_items1_2">
    <property name="geometry">
     <rect>
      <x>230</x>
      <y>60</y>
      <width>21</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>to:</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>861</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

邏輯是:

  • 創建一個AxesSubplot
  • 打掃,
  • 調用實現 plot 的 function (在您的情況下為pie( ))和
  • 調用draw()來繪制它。
    # ...
    self.date_to.dateChanged.connect(self.fcn_date_changed)

    fig = Figure()
    self.canvas = FigureCanvas(fig)
    self.mplvl.addWidget(self.canvas)
    self.ax1 = fig.add_subplot(111)
    self.ax1.pie([])

def fcn_date_changed(self):
    self.ax1.clear()
    self.ax1.set_title("Percentage Spending by Category")
    print("from: " + self.date_from.text() + " to: " + self.date_to.text())
    labels = "Frogs", "Hogs", "Dogs", "Logs"
    sizes = [15, 30, 45, 10]
    self.ax1.pie(
        sizes, labels=labels, autopct="%1.1f%%", shadow=False, startangle=90
    )
    self.ax1.figure.canvas.draw()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM