how to create a pie chart with pyqt in python

泄露秘密 提交于 2019-12-22 14:14:46

问题


Trying to create a pie chart shape but for some reason I can't get it to join together correctly. When I run my code It creates a lot of segments on top of each other. Here is my code:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, random


app = QApplication(sys.argv)
scene = QGraphicsScene()
families = [1,2,3,4,5,6,7]
total = 0
colours = []
set_angle = 0
count1 = 0
total = sum(families)
for count in range(len(families)):
    number = []
    for count in range(3):
        number.append(random.randrange(0, 255))
    colours.append(QColor(number[0],number[1],number[2]))


for family in families:    
    angle = round(family/total*16*360)
    ellipse = QGraphicsEllipseItem(0,0,400,400)
    ellipse.setPos(0,0)
    ellipse.setStartAngle(set_angle)
    ellipse.setSpanAngle(angle)
    ellipse.setBrush(colours[count1])
    set_angle = angle
    count1 +=1
    scene.addItem(ellipse)
view = QGraphicsView(scene)
view.show()
app.exec_()

回答1:


Digging this thread as it might help others. @Barry's answer is almost correct, but needs a few adjustments.

Indeed, to have a perfect round shape to your ellipse, you need to change the line:

set_angle = angle

to

set_angle += angle

This way, the set_angle (which is the starting angle of our "pie chunks") is always the last angle drawn on the canvas.

Also, the line:

angle = round(family/total*16*360)

could be written like this (for readability's sake):

angle = round(float(family*(16*360))/total)

So a working example would be (using Python3 & PyQt5):

from PyQt5.QtWidgets import QGraphicsScene, QApplication, QGraphicsView, QGraphicsEllipseItem
from PyQt5.Qt import QColor
import sys, random


app = QApplication(sys.argv)
scene = QGraphicsScene()

families = [1,2,3,4,5,6,7,8,9,10]
total = 0
set_angle = 0
count1 = 0
colours = []
total = sum(families)

for count in range(len(families)):
    number = []
    for count in range(3):
        number.append(random.randrange(0, 255))
    colours.append(QColor(number[0],number[1],number[2]))

for family in families:
    # Max span is 5760, so we have to calculate corresponding span angle
    angle = round(float(family*5760)/total)
    ellipse = QGraphicsEllipseItem(0,0,400,400)
    ellipse.setPos(0,0)
    ellipse.setStartAngle(set_angle)
    ellipse.setSpanAngle(angle)
    ellipse.setBrush(colours[count1])
    set_angle += angle
    count1 += 1
    scene.addItem(ellipse)

view = QGraphicsView(scene)
view.show()
app.exec_()

Hope it helps.




回答2:


The problem is related to integer division, check this line: angle = round(family/total*16*360), it could be fixed setting total = float(sum(families)) or putting this line from __future__ import division at the start of your code, both solutions are valid.




回答3:


as xndrme said you need

from __future__ import division

Your segments however will still be on top of each other because they all have the same starting angle.

set_angle = angle

should be

set_angle = angle + angle


来源:https://stackoverflow.com/questions/21944696/how-to-create-a-pie-chart-with-pyqt-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!