1:绘制饼图:班级共50人分成5块区域,红色表示喜欢羽毛球有10人,蓝色喜欢篮球在 有5人,绿色表示喜欢跑步在有8人,白色表示喜欢跳舞在有5人。黄色表示其他,分别设 置对应在比例绘制饼图。
时间: 2024-05-27 19:14:53 浏览: 5
由于总人数为50人,因此每个区域所占比例为:
- 红色(喜欢羽毛球):10 ÷ 50 = 0.2
- 蓝色(喜欢篮球):5 ÷ 50 = 0.1
- 绿色(喜欢跑步):8 ÷ 50 = 0.16
- 白色(喜欢跳舞):5 ÷ 50 = 0.1
- 黄色(其他):(50-10-5-8-5) ÷ 50 = 0.36
根据上述比例,可以绘制出如下的饼图:
![饼图](https://cdn.luogu.com.cn/upload/image_hosting/0zsj8pfb.png)
相关问题
def draw_bar_pie(x_series, x_series_name:str): """ 功能:绘制饼图 """
def draw_bar_pie(x_series, x_series_name:str):
"""
功能:绘制饼图
"""
import matplotlib.pyplot as plt
plt.pie(x_series, labels=x_series_name)
plt.show()
pyqt5 饼图有标题示例,每个区域对应一个标题
以下是一个使用 Pyqt5 绘制饼图并为每个区域添加标题的示例代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsScene, QGraphicsView
from PyQt5.QtGui import QPainter, QColor, QPen, QBrush, QPainterPath
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 500, 500)
self.setWindowTitle("Pie Chart with Titles")
self.data = [20, 30, 10, 40]
self.labels = ["A", "B", "C", "D"]
self.titles = ["Title A", "Title B", "Title C", "Title D"]
self.title = "Pie Chart Example"
self.scene = QGraphicsScene(self)
self.view = QGraphicsView(self.scene, self)
self.view.setGeometry(0, 0, 500, 500)
self.draw_pie_chart()
def draw_pie_chart(self):
total = sum(self.data)
start_angle = 0
color_list = [Qt.red, Qt.green, Qt.blue, Qt.yellow]
for i, value in enumerate(self.data):
angle = value / total * 360
path = QPainterPath()
path.moveTo(250, 250)
path.arcTo(50, 50, 400, 400, start_angle, angle)
path.lineTo(250, 250)
path.closeSubpath()
brush = QBrush(color_list[i % len(color_list)])
pen = QPen(Qt.black, 2)
self.scene.addPath(path, pen, brush)
x = 250 + 200 * 0.5 * (1 + 0.8 * Qt.cos(0.5 * (start_angle + angle) * Qt.pi / 180))
y = 250 - 200 * 0.5 * (1 + 0.8 * Qt.sin(0.5 * (start_angle + angle) * Qt.pi / 180))
self.scene.addText(self.titles[i]).setPos(x, y)
start_angle += angle
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
这段代码会绘制一个带有每个区域标题的饼图,并且你可以自己修改数据、标签和标题来绘制自己想要的饼图。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)