import plotly.graph_objects as go
import plotly.io as pio
# pio.renderers.default = 'png'
# pio.renderers.default = "browser"
fig = go.Figure(
data=[go.Bar(y=[2, 1, 3, 4])],
layout_title_text="A Figure Displayed with fig.show()"
)
fig.layout = go.Layout(
autosize=False,
width=500,
height=500
)
# fig.show()
fig.show(renderer='browser', )
fig.write_image("images/fig1.png")
import numpy as np
from plotly.offline import plot # offline plot
import plotly.graph_objs as go
import matplotlib.pyplot as plt
refresh_time = 2 # seconds
def create_a_trace():
N = 10
trace = go.Scatter(
x=np.random.randn(N),
y=np.random.randn(N),
mode='markers'
)
return trace
def write_to_html(data, index):
html = '<meta http-equiv="refresh" content="' + str(refresh_time) + '" >' # add auto refresh html meta data
html = html + '<h3> plot '+str(index)+'</h3>'
html = html + plot(data, include_plotlyjs=True, output_type='div')
# html = '<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>' + html
html = '<html>' + html + '</html>'
# Write HTML String to html file
with open("basic_refresh.html", "w") as file:
file.write(html)
# initial plot
data = [create_a_trace()]
write_to_html(data, 0)
# update plot
for i in range(20):
plt.pause(refresh_time) # time.sleep() does not work in here
data = [create_a_trace()]
write_to_html(data, i)