首页 > 解决方案 > 如何在不重新生成图形的情况下离线使用情节?

问题描述

目前,我正在像这样离线使用情节:

from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import *
import plotly.graph_objects as go
import plotly

import numpy as np


class Window(QWidget):

    def __init__(self):
        super(Window, self).__init__()

        grid_layout = QGridLayout()

        self.pb = QPushButton('plot')

        # some example data
        x = np.arange(0, 2*np.pi, 0.001)
        y = np.sin(x)

        # create the plotly figure
        fig = go.Figure(go.Scatter(x=x, y=y))

        # we create html code of the figure
        html = "".join(['<html><body>',
                        plotly.offline.plot(fig, output_type='div', include_plotlyjs='cdn'),
                        '</body></html>']
                       )

        # we create an instance of QWebEngineView and set the html code
        self.plot_widget = QWebEngineView()
        self.plot_widget.setHtml(html)

        grid_layout.addWidget(self.plot_widget, 0, 0)
        grid_layout.addWidget(self.pb, 1, 0)

        self.setLayout(grid_layout)

        self.pb.clicked.connect(self.newplot)

    def newplot(self):
        # some example data
        x = np.arange(0, 2 * np.pi, 0.001)
        y = np.sin(x+np.random.uniform(low=0, high=2*np.pi))

        # create the plotly figure
        fig = go.Figure(go.Scatter(x=x, y=y))

        # we create html code of the figure
        html = "".join(['<html><body>',
                        plotly.offline.plot(fig, output_type='div', include_plotlyjs='cdn'),
                        '</body></html>']
                       )
        self.plot_widget.setHtml(html)


if __name__ == '__main__':
    app = QApplication([])
    window = Window()
    window.show()
    app.exec_()

我的问题是,这样当我画一个新的情节时,整个事情都会重新生成。有没有办法更顺利地做到这一点,只更新现有的数字?保留轴,并在图中用新线替换旧线。

标签: pythonplotpyqt5plotlyoffline

解决方案


我已经使用 Plotly for Python 一年多了,但我从未听说过你描述的可能性。


推荐阅读