QT5 練習之 餅圖顯示

# Time: 2020/05/29

#Author: Xiaohong

# 運行環境: OS: Windows 10

#  Python: 3.7

# 功能: 把一個列表數據通過餅圖顯示

效果爲:

主程序如下:

# PIE 窗口
class PI_Pie(QDialog):
    def __init__(self,parent=None, datadic=None):
        super(PI_Pie,self).__init__(parent)

        self.figure = plt.figure(figsize=(20,20))
        self.canvas = FigureCanvas(self.figure)

        # self.figure_date = plt.figure()
        # self.canvas_date = FigureCanvas(self.figure_date)

        self.child= Ui_Q_pi_pie.Ui_Dialog_draw_pie()
        self.child.setupUi(self)     
        self.child.bnt_show.clicked.connect(self.draw_pie_total_pc)         

    def draw_pie_total_pc(self):
        self.child.horizontalLayout.removeWidget(self.canvas)
        self.CreatePieCharts_total_pc()
        self.child.horizontalLayout.addWidget(self.canvas)   
        pass

    def CreatePieCharts_total_pc(self):
        plt.cla()   # Clear axis即清除當前圖形中的當前活動軸。其他軸不受影響。
        plt.clf()   # Clear figure清除所有軸,但是窗口打開,這樣它可以被重複使用。
        # plt.title('不良類型佔比', fontsize='18', fontweight='bold',
        #              color='black', loc='center',bbox={'facecolor': '0.8', 'pad': 5})
        
        self.results={}
        self.results["Normal_PC"] = 8
        self.results["Virus_PC"] = 2

        print(self.results)        

        pieDataList = []
        labels = []
        explode = []
        for item in self.results.keys():
            labels.append(item)   #是標籤 每一塊餅圖外側顯示的說明文字
            pieDataList.append(self.results[item])    #是數據
            explode.append(0.04)   #explodes 爲0 代表不偏離圓心, 不爲零則代表偏離圓心的距離
        print(labels)
        print(pieDataList)
        print(explode)
        # startangle :起始繪製角度,默認圖是從x軸正方向逆時針畫起,如設定startangle=90則從y軸正方向畫起; 
        # labeldistance : label繪製位置,相對於半徑的比例, 如<1則繪製在餅圖內側,默認值爲1.1
        # autopct :控制餅圖內百分比設置,可以使用format字符串或者format function
        # radius :控制餅圖半徑;浮點類型,可選參數,默認爲:None。如果半徑是None,將被設置成1。 radius :控制餅圖半徑;浮點類型,可選參數,默認爲:None。如果半徑是None,將被設置成1。 
        # pctdistance : 類似於labeldistance,指定autopct的位置刻度,默認值爲0.6;
        # textprops :設置標籤(labels)和比例文字的格式;字典類型,可選參數,默認值爲:None。
        plt.pie(pieDataList,labels=labels, radius=0.8,startangle=90, explode= tuple(explode),autopct = '%3.1f%%',pctdistance=0.6,labeldistance=0.5,textprops={'fontsize':8,'color':'black'})
        plt.axis('equal')    #將餅圖顯示爲正圓形
        # plt.legend(loc=2)  #添加圖例
        self.canvas.draw()

以如下方式調用 Class (需要自行寫程序調用 如下函數 Call_Draw_Pie):

def Call_Draw_Pie(self):        
        self.results={}
        self.results["Total_PC"] = 10
        self.results["Virus_PC"] = 2
        self.pi_pie = PI_Pie(self, datadic=self.results)  
        self.pi_pie.exec()

用QT5 設計的顯示餅圖程序如下(Q_pi_pie.ui):

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog_draw_pie</class>
 <widget class="QDialog" name="Dialog_draw_pie">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>976</width>
    <height>746</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>顯示圖片</string>
  </property>
  <widget class="QPushButton" name="bnt_show">
   <property name="geometry">
    <rect>
     <x>400</x>
     <y>30</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>Show Total</string>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton_2">
   <property name="geometry">
    <rect>
     <x>470</x>
     <y>30</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>btn_exit</string>
   </property>
  </widget>
  <widget class="QWidget" name="horizontalLayoutWidget">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>70</y>
     <width>541</width>
     <height>381</height>
    </rect>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout"/>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>pushButton_2</sender>
   <signal>clicked()</signal>
   <receiver>Dialog_draw_pie</receiver>
   <slot>close()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>335</x>
     <y>44</y>
    </hint>
    <hint type="destinationlabel">
     <x>269</x>
     <y>118</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章