๐ฉ๐ป ์ธ๊ณต์ง๋ฅ (ML & DL)/Serial Data
Dash์ Python์ ์ฌ์ฉํ์ฌ ์ค์๊ฐ ์ ๋ฐ์ดํธ ๊ทธ๋ํ๋ฅผ ์์ฑ
์ง์ง์ํ์นด
2022. 11. 14. 13:15
728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ pythonprogramming_net ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค>
https://pythonprogramming.net/live-graphs-data-visualization-application-dash-python-tutorial/
Python Programming Tutorials
Live Graphs - Data Visualization GUIs with Dash and Python p.4 Welcome to part four of the web-based data visualization with Dash tutorial series. In this tutorial, we're going to be create live updating graphs with Dash and Python. Live graphs can be usef
pythonprogramming.net
๐ Dash ๊ธฐ๋ณธ ๊ตฌ์กฐ
- dash lib, ๋ค์ํ ๊ตฌ์ฑ ์์(๊ทธ๋ํ ๊ตฌ์ฑ ์์์ ๊ฐ์ ๊ฒ), HTML ๊ตฌ์ฑ ์์(div ํ๊ทธ ๋ฑ...๋ฑ)์ ๊ฐ์ ๊ฒ์ ๊ฐ์ ธ์ด
- Dash๋ Flaskํ๋ ์์ํฌ๋ฅผ ์ค์ฌ์ผ๋ก ๊ตฌ์ถ
import dash
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div('Dash Tutorials')
if __name__ == '__main__':
app.run_server(debug=True)

๐ ์ค์๊ฐ ์ ๋ฐ์ดํธ ๊ทธ๋ํ
import dash
from dash.dependencies import Output, Input
import dash_core_components as dcc
import dash_html_components as html
import plotly
import random
import plotly.graph_objs as go
from collections import deque
X = deque(maxlen=20)
X.append(1)
Y = deque(maxlen=20)
Y.append(1)
# ์์์ ์์ง์์ ์ถ๊ฐํ์ฌ ์ผ๋ถ ๋ฐ์ดํฐ๋ฅผ ์๋ฎฌ๋ ์ด์
app = dash.Dash(__name__)
app.layout = html.Div(
[
dcc.Graph(id='live-graph', animate=True),
dcc.Interval(
id='graph-update',
interval=1*1000
),
]
)
# ์์์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐ
@app.callback(Output('live-graph', 'figure'),
[Input('graph-update', 'n_intervals')])
def update_graph_scatter(input_data):
X.append(X[-1]+1)
Y.append(Y[-1]+Y[-1]*random.uniform(-0.1,0.1))
# ํจ์๊ฐ ์คํ๋ ๋๋ง๋ค ๋ช ๊ฐ์ง ์๋ก์ด ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ์ผ๋ฏ๋ก ๊ณ์ํด์ ๊ทธ๋ํ๋ฅผ ์์ฑํ๋ ค๊ณ ํจ
data = plotly.graph_objs.Scatter(
x=list(X),
y=list(Y),
name='Scatter',
mode= 'lines+markers'
)
# x์ y์ ๋ํ ๋ชฉ๋ก์ ์ ๋ฌํด์ผ ํ๋ฏ๋ก deque๊ฐ์ฒด๋ฅผ ์ ์งํ ์ ์์
return {'data': [data],'layout' : go.Layout(xaxis=dict(range=[min(X),max(X)]),
yaxis=dict(range=[min(Y),max(Y)]),)}
if __name__ == '__main__':
app.run_server(host='0.0.0.0', port=8080 ,debug=True)
๊ฐ์ด ๋์ ๋์ง ์์
728x90
๋ฐ์ํ