Real-Time Data Visualizations Using Python Library — Plotly

Handhika Yanuar Pratama
3 min readFeb 11, 2024

--

Photo by Aron Visuals on Unsplash

Playing with accurate data is very necessary today. In the emerging world, old data is good, but real-time data is urgently needed. This story will tell you about one Python library, Plotly, that can work with real-time data.

The Requirements

This example uses Pandas for data manipulation, Plotly Express to create a line chart, and Dash to create a simple web application. Make sure to install the necessary libraries.

pip install pandas plotly dash

The Data

For a more accurate example, let’s assume your Excel file contains Timestamp and Value columns. The Timestamp column should contain datetime values, and the Value column should contain numeric values you want to visualize in real time.

You could adjust it with whatever data you need. Ensure that the Timestamp column is formatted as datetime in Excel; it is necessary since it’s real-time data.

The Script

To create a real-time data visualization using Plotly and take data from an Excel file, we can use Python and libraries like Pandas for data manipulation and Plotly for interactive plotting. We may use a loop to update the plot with new data for continuous real-time updates.

import pandas as pd
import plotly.express as px
from datetime import datetime
from dash import Dash, html, dcc
from dash.dependencies import Input, Output
import dash

# Load initial data from Excel file
excel_file_path = 'data.xlsx'
df = pd.read_excel(excel_file_path)

# Create a Dash web application
app = Dash(__name__)

# Define the layout of the application
app.layout = html.Div([
dcc.Graph(id='real-time-plot'),
dcc.Interval(
id='interval-component',
interval=10*1000, # in milliseconds
n_intervals=0
)
])

# Define callback to update the plot in real-time
@app.callback(Output('real-time-plot', 'figure'),
[Input('interval-component', 'n_intervals')])

def update_graph(n_intervals):
# Load new data from Excel file
new_data = pd.read_excel(excel_file_path)

# Update the plot
fig = px.line(new_data, x='Timestamp', y='Value', title='Real-time Data Plot')

return fig

# Run the application
if __name__ == '__main__':
app.run_server(debug=True)

Suppose you are confused about what @app.callback is used for. In this case, the @app.callbacksays: “Whenever the n_intervals property of the interval-component changes (which happens at regular intervals), update the figure property of the real-time-plot component by calling the update_graph function.” The update_graph function, in turn, reads new data from the Excel file and updates the plot accordingly.

The Conclusions

If you run the program, it will run the Dash; you can access it within the URL displayed on the screen.

Here’s the result

The Data will be updated accordingly whenever the Excel data is updated. Thanks for reading. Happy Learning 😇

--

--

Handhika Yanuar Pratama
Handhika Yanuar Pratama

Written by Handhika Yanuar Pratama

Live the Way the Life ask for || A Stoic Engineer || Technical Writer || Runner x Dreamer

No responses yet