我正在尝试使用 Plotly Dash 和 Python 创建一个实时更新图。有没有办法删除以前的(第一个数据输入)数据点并只绘制传入的数据点?这是我的代码:
import pandas as pd
import dash
import plotly.graph_objects as go
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash(__name__)
app.title = "Sistem de detectie al punctului mort"
def _create_fig():
df = pd.read_csv('/home/pi/tflite1/csv_data_file.csv')
df.columns=['x','y']
return go.Figure(
data=go.Scatter(
x=df['x'],
y=df['y'], mode = 'markers'))
app.layout = html.Div([
dcc.Graph(
id='g1',
animate = True,
figure=_create_fig()),
dcc.Interval(
id='interval-component',
interval=1*1000, # in milliseconds
n_intervals=0
)
])
#
#
@app.callback(
dash.dependencies.Output('g1', 'figure'),
dash.dependencies.Input('interval-component', 'n_intervals')
)
def refresh_data(n_clicks):
return _create_fig()
if __name__ == "__main__":
app.run_server(host='0.0.0.0', debug=True, port=8099)
很晚的答案,但可能对某人有用:
仅显示“传入点”或最后一个点:您可以添加仅包含数组最后一个值的迹线。 例如您的代码:
def _create_fig():
df = pd.read_csv('/home/pi/tflite1/csv_data_file.csv')
df.columns=['x','y']
return go.Figure(
data=go.Scatter(
#x=df['x']
x=df['x'].iloc[-1:],
#y=df['y']
y=df['y'].iloc[-1:], mode = 'markers'))
它与“.iloc[-1:]”一起使用,就我而言,当我编写“.iloc[-1]”时出现错误,因此“:”很重要。