PyQt5绘图多条折线图
时间: 2023-10-09 19:15:36 浏览: 151
Python折线图绘制
在PyQt5中实现绘制多条折线图的方法如下:
1. 导入PyQt5的相应模块:
```python
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt
```
2. 创建继承自QWidget的自定义类,重写其paintEvent方法:
```python
class Demo(QWidget):
def __init__(self, parent=None):
super(Demo, self).__init__(parent)
self.setWindowTitle("PyQt5绘制多条折线图")
self.resize(400, 300)
def paintEvent(self, event):
painter = QPainter(self)
pen = QPen(Qt.black, 2, Qt.SolidLine)
painter.setPen(pen)
points_list = [[50, 50], [100, 150], [150, 100], [200, 200], [250, 150], [300, 250]]
for i in range(len(points_list) - 1):
x1, y1 = points_list[i]
x2, y2 = points_list[i + 1]
painter.drawLine(x1, y1, x2, y2)
```
3. 创建QApplication对象并运行应用程序:
```python
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
demo = Demo()
demo.show()
sys.exit(app.exec_())
```
以上代码实现了在窗口中绘制了多条折线图,折线图的坐标点分别为[[50, 50], [100, 150], [150, 100], [200, 200], [250, 150], [300, 250]]。每两个坐标点之间使用drawLine方法绘制一条直线。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [八、PyQt5绘图技术](https://blog.csdn.net/ungoing/article/details/127257307)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文