Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i need to plot a piechart in PyQt4 GUI using MatPlotLib

EDITED: i was able to get it working as thus:

import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from PyQt4 import QtCore, QtGui
from ui_ageingReport import Ui_report_ageingDisplay

class AgeingChart():

    def __init__(self, label, frac, titl):
        self.age_dialog=QtGui.QDialog()
        self.age_ui = Ui_report_ageingDisplay()
        self.age_ui.setupUi(self.age_dialog)


        self.dpi = 120
        self.fig = plt.figure(1, figsize=(4,4))
        self.fig.add_subplot(111)
        explode=(0, 0.05, 0, 0, 0)
        labels = label
        fracs = frac
        plt.pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
        plt.title(titl, bbox={'facecolor':'0.8', 'pad':10})
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setParent(self.age_ui.chart)
        self.age_dialog.exec_()

the only problem is positioning it please take a look at the image below, it currently looks like the first image, but i want it to look like the second image i photoshoped the second image, thats why some text are chopped off i need more room at the sides to allow for labeling.

Thanks

some how i cant upload images here is a link to the image http://www.somans.com/Untitled-1.jpg

like image 954
Ajayi Oluwaseun Emmanuel Avatar asked Sep 11 '26 20:09

Ajayi Oluwaseun Emmanuel


1 Answers

Sounds like your question is, "how can I make the pie chart sit in the middle of my figure?". Which I will answer:

There is nothing special about the fact that you are using a QT4 windowing system to produce your plot. I have taken the matplotlib pie chart example, and modified the figure width to emulate your requirements. Finally, and crucially, I set the aspect ratio to be 1:1 to keep the pie chart circular:

from pylab import *

# make a square figure and axes
figure(1, figsize=(10, 3))
ax = axes([0.1, 0.1, 0.8, 0.8])

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

explode=(0, 0.05, 0, 0)
pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})

gca().set_aspect('1')
show()

enter image description here

like image 92
pelson Avatar answered Sep 13 '26 12:09

pelson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!