如何在不再生图形的情况下离线使用plot ?



目前,我正在离线使用plot:

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

我的问题是,这样当我画一个新的图,整个东西再生。有没有办法做得更顺利,只更新现有的数字?保留坐标轴,只是用新的线替换旧的线。

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

最新更新