简体   繁体   English

我需要使用MatPlotLib在PyQt4 GUI中绘制一个饼图

[英]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. 唯一的问题是定位它请看看下面的图像,它目前看起来像第一个图像,但我希望它看起来像第二个图像我photoshoped第二个图像,这就是为什么一些文字被砍掉我需要更多的空间在两侧允许标签。

Thanks 谢谢

some how i cant upload images here is a link to the image http://www.somans.com/Untitled-1.jpg 一些我无法上传图像的方法是图片的链接http://www.somans.com/Untitled-1.jpg

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. 您正在使用QT4窗口系统来制作您的情节这一事实没有什么特别之处。 I have taken the matplotlib pie chart example, and modified the figure width to emulate your requirements. 我已经采用了matplotlib饼图示例,并修改了图形宽度以模拟您的要求。 Finally, and crucially, I set the aspect ratio to be 1:1 to keep the pie chart circular: 最后,至关重要的是,我将宽高比设置为1:1以保持饼图为圆形:

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()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM