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

3

编辑后: 我能够这样让它工作:

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

唯一的问题是定位它。请看下面的图片,它目前看起来像第一张图片,但我想让它看起来像第二张图片。我用Photoshop修过第二张图片,所以有些文字被裁剪了。我需要更多的空间来容纳标签。
谢谢。
某种方式我无法在这里上传图片,这里是图片链接:http://www.somans.com/Untitled-1.jpg

1
这非常嘈杂。问题是什么? - pelson
请在此处查看图片,我目前所拥有的是第一张图片中的内容,我需要实现第二张图片中的内容。这是链接:http://www.somans.com/Untitled-1.jpg。 - Ajayi Oluwaseun Emmanuel
1个回答

4

看起来你的问题是:“我怎样让饼图位于我的图形中央?”。我会回答:

你使用QT4窗口系统制作绘图并没有什么特别之处。我使用了matplotlib的饼图示例,并修改了图形宽度以符合你的要求。最后,关键是我将纵横比设置为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()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接