Я новичок в программировании. У меня есть некоторые, как до сих пор придумали приличный график. Мой график Live обновляется каждые 5 секунд с новыми данными для следов. Как я могу предотвратить потерю позиции увеличения при обновлении графика?
import dash
from dash.dependencies import Output, Input, State
import dash_core_components as dcc
import dash_html_components as html
import plotly
import plotly.graph_objs as go
import csv
import itertools
import datetime as dt
import attr
@attr.s(frozen=True)
class Plot:
xs = attr.ib()
ys = attr.ib()
def _humidity():
with open("z:/Humidity Log.txt", "r") as csvfile:
reader = csv.reader(csvfile)
plots = itertools.islice(reader, None, None, 1)
return Plot(
*zip(*((dt.datetime.fromisoformat(x), float(y)) for x, y, *_ in plots))
)
def _temp():
with open("Z:/Temp Log.txt", "r") as csvfile:
reader = csv.reader(csvfile)
plots = itertools.islice(reader, None, None, 1)
return Plot(
*zip(*((dt.datetime.fromisoformat(x), float(y)) for x, y, *_ in plots))
)
def _auxInput():
with open("Z:/Temp Log.txt", "r") as csvfile:
reader = csv.reader(csvfile)
plots = itertools.islice(reader, None, None, 20)
return Plot(
*zip(*((dt.datetime.fromisoformat(x), float(y)) for x, y, *_ in plots))
)
def auxinput(n):
aux = _auxInput()
return {
"data": [
plotly.graph_objs.Scatter(
x=list(aux.xs),
y=list(aux.ys),
line={"color": "#008f13"},
name="A/C Run-time",
mode="lines+markers",
),
],
"layout": go.Layout(
uirevision="null",
height=500,
plot_bgcolor="rgb(219, 219, 219)",
xaxis={
"type": "date",
"range": [min(aux.xs), max(aux.xs)],
},
yaxis_tickwidth=2,
xaxis_tickangle=45,
yaxis_linewidth=1,
xaxis_linewidth=1,
xaxis_tickformatstops=[
dict(dtickrange=[None, 1000], value="%H:%M:%S.%L ms"),
dict(dtickrange=[1000, 60000], value="%H:%M:%S s"),
dict(dtickrange=[60000, 3600000], value="%H:%M m"),
dict(dtickrange=[3600000, 86400000], value="%H:%M h"),
dict(dtickrange=[86400000, 604800000], value="%e. %b d"),
dict(dtickrange=[604800000, "M1"], value="%e. %b w"),
dict(dtickrange=["M1", "M12"], value="%b '%y M"),
dict(dtickrange=["M12", None], value="%Y Y")
],
yaxis={"title": "A/C Run-time",
'autorange': True,
"range": [min(aux.ys), max(aux.ys)]},
),
}
def temperature(n):
temp = _temp()
return {
"data": [
plotly.graph_objs.Scatter(
x=list(temp.xs),
y=list(temp.ys),
line={"color": "#f51000"},
name="Temperature",
mode="lines+markers",
),
],
"layout": go.Layout(
uirevision='any',
height=500,
plot_bgcolor="rgb(219, 219, 219)",
xaxis={
"type": "date",
"range": [min(temp.xs), max(temp.xs)],
},
yaxis_tickwidth=2,
xaxis_tickangle=45,
yaxis_linewidth=1,
xaxis_linewidth=1,
xaxis_tickformatstops=[
dict(dtickrange=[None, 1000], value="%H:%M:%S.%L ms"),
dict(dtickrange=[1000, 60000], value="%H:%M:%S s"),
dict(dtickrange=[60000, 3600000], value="%H:%M m"),
dict(dtickrange=[3600000, 86400000], value="%H:%M h"),
dict(dtickrange=[86400000, 604800000], value="%e. %b d"),
dict(dtickrange=[604800000, "M1"], value="%e. %b w"),
dict(dtickrange=["M1", "M12"], value="%b '%y M"),
dict(dtickrange=["M12", None], value="%Y Y")
],
yaxis={"title": "Temperature",'fixedrange': True,'autorange': True,"range": [min(temp.ys), max(temp.ys)]},
),
}
def humidity(n):
humidity = _humidity()
return {
"data": [
plotly.graph_objs.Scatter(
x=list(humidity.xs),
y=list(humidity.ys),
line={"color": "#0539f7"},
name="Humidity",
mode="lines+markers",
),
],
"layout": go.Layout(
uirevision="null",
height=500,
plot_bgcolor="rgb(219, 219, 219)",
yaxis_tickwidth = 2,
xaxis_tickangle = 45,
yaxis_linewidth=1,
xaxis_linewidth=1,
xaxis_tickformatstops=[
dict(dtickrange=[None, 1000], value="%H:%M:%S.%L ms"),
dict(dtickrange=[1000, 60000], value="%H:%M:%S s"),
dict(dtickrange=[60000, 3600000], value="%H:%M m"),
dict(dtickrange=[3600000, 86400000], value="%H:%M h"),
dict(dtickrange=[86400000, 604800000], value="%e. %b d"),
dict(dtickrange=[604800000, "M1"], value="%e. %b w"),
dict(dtickrange=["M1", "M12"], value="%b '%y M"),
dict(dtickrange=["M12", None], value="%Y Y")
],
xaxis={"type": "date","range": [min(humidity.xs), max(humidity.xs)],},
yaxis={"title": "Humidity", 'fixedrange': True, "range": [min(humidity.ys), max(humidity.ys)]},
),
}
app = dash.Dash(__name__)
app.title = 'Temp/Humidity Graph'
app.layout = html.Div([
dcc.Graph(
id="live-temperature",
animate=False,
config={"displaylogo": False,
"scrollZoom": True,
"modeBarButtonsToRemove": ['lasso2d',
'toggleSpikelines']},
figure=temperature(0),
),
dcc.Graph(
id="live-humidity",
animate=False,
config={"displaylogo": False,
"scrollZoom": True,
"modeBarButtonsToRemove": ['lasso2d',
'toggleSpikelines']},
figure=humidity(0),
),
dcc.Graph(
id="live-auxInput",
animate=False,
config={"displaylogo": False,
"scrollZoom": True,
"modeBarButtonsToRemove": ['lasso2d',
'toggleSpikelines']},
figure=auxinput(0),
),
dcc.Interval(id="graph-update", interval=1 * 5000),
]
)
@app.callback(
Output("live-temperature", "figure"),
[Input("graph-update", "n_intervals")],
)
def update_graph_scatter_temp(n):
return temperature(n)
@app.callback(
Output("live-humidity", "figure"),
[Input("graph-update", "n_intervals")],
)
def update_graph_scatter_humidity(n):
return humidity(n)
@app.callback(
Output("live-auxInput", "figure"),
[Input("graph-update", "n_intervals")],
)
def update_graph_scatter_aux(n):
return auxinput(n)
if __name__ == "__main__":
app.run_server(host="0.0.0.0", port=8051, debug=False)
Я использовал uirevision, и эта помощь сохраняет масштабирование при обновлении графика без новых данных. Я видел несколько примеров и демонстраций того, как это сделать, но я не могу держать вас с ними, потому что мои знания ограничены.