jumeaux-numeriques/twin_plotly.py

74 lines
2.5 KiB
Python
Raw Normal View History

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
import plotly.express as px
import numpy as np
# import plotly.graph_objects as go
import random
import time
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.browser = QtWebEngineWidgets.QWebEngineView(self)
vlayout = QtWidgets.QVBoxLayout(self)
vlayout.addWidget(self.browser)
self.show_graph()
# self.resize(1000,800)
self.resize(500,400)
def show_graph(self):
# fig = go.Figure()
# xdata = [0]
# ydata = [0]
# df = px.data.tips()
# fig = px.box(df, x="day", y="total_bill", color="smoker")
# fig.update_traces(quartilemethod="exclusive") # or "inclusive", or "linear" by default
# fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2])
# fig.add_trace(go.Scatter(x=xdata, y=ydata, name='essai',line=dict(color='firebrick', width=4)))
# for i in range (100):
# xdata.append(i)
# ydata.append(random.randint(0, 10))
# # fig.update_traces()
# # self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
# # fig.add_trace(go.Scatter(x=xdata, y=ydata, name='essai',line=dict(color='firebrick', width=4)))
# ydata.append(random.randint(0, 10))
# fig.update_traces()
# self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
# initialize and display plot
# fig = go.FigureWidget()
# fig.add_scatter(y=np.random.randint(0,10, 5), fill='tozeroy')
# display(fig)
xdata=[0]
ydata=[0]
fig = px.scatter(x=xdata, y=ydata)
# fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
# print (fig.data[0].x)
# print (fig.data[0].y)
# fig.show()
self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
# modify plot using new data
for i in range(10):
time.sleep(0.5)
xdata.append(i+1)
ydata.append(random.randint(0, 10))
fig.update_traces()
# np.append(fig.data[0].x, i+2)
# np.append(fig.data[0].y, np.random.randint(0,10, 5))
# fig.update_traces()
# fig.data[0].y.append(np.random.randint(0,10, 5))
# fig.data[0].y = np.random.randint(0,10, 5)
# fig.data[0].y = np.random.randint(0,10, 5)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = Widget()
widget.show()
app.exec()