Introduction to Interactive Time Series Visualizations with Plotly in Python

We can also click Edit Chart and open it up in the online editor to make any changes we want in an easy-to-use interface:Online editor interfaceIf you edit the chart in the online editor you can then automatically generate the Python code for the exact graph and style you have created!Improving PlotsEven a basic time-series plot in Plotly is impressive but we can improve it with a few more lines of code..For example, let’s say we want to compare the steam usage of the building with the energy..These two quantities have vastly different units, so if we show them on the same scale it won’t work out..This is a case where we have to use a secondary y-axis..In matplotlib, this requires a large amount of formatting work, but we can do it quite easily in Plotly.The first step is to add another data source, but this time specify yaxis='y2'.# Get the steam datasteam_series = df.loc[:, ("Steam", "4")]# Create the steam data objectsteam_data = go.Scatter(x=steam_series.index, y=steam_series.values, # Specify axis yaxis='y2')(We also add in a few other parameters to improve the styling which can be seen in the notebook).Then, when we create the layout, we need to add a second y-axis.layout = go.Layout(height=600, width=800, title='Energy and Steam Plot', # Same x and first y xaxis=dict(title='Date'), yaxis=dict(title='Energy', color='red'), # Add a second yaxis to the right of the plot yaxis2=dict(title='Steam', color='blue', overlaying='y', side='right') )fig = go.Figure(data=[energy_data, steam_data], layout=layout)py.iplot(fig, sharing='public')When we display the graph, we get both steam and energy on the same graph with properly scaled axes.With a little online editing, we get a finished product:Finished plot of energy and steam.Plot AnnotationsPlot annotations are used to call out aspects of a visualization for attention..As one example, we can highlight the daily high consumption of steam while looking at a week’s worth of data..First, we’ll subset the steam sensor into one week (called steam_series_four) and create a formatted data object:# Data objectsteam_data_four = go.Scatter( x=steam_series_four.index, y=steam_series_four.values, line=dict(color='blue', width=1.1), opacity=0.8, name='Steam: Sensor 4', hoverinfo = 'text', text = [f'Sensor 4: {x:.1f} Mlbs/hr' for x in steam_series_four.values])Then, we’ll find the daily max values for this sensor (see notebook for code):To build the annotations, we’ll use a list comprehension adding an annotation for each of the daily maximum values ( four_highs is the above series)..Each annotation needs a position ( x , y) and text:# Create a list of annotationsfour_annotations = [dict(x = date, y = value, xref = 'x', yref = 'y', font=dict(color = 'blue'), text = f'{format_date(date)}<br> {value[0]:.1f} Mlbs/hr') for date, value in zip(four_highs.index, four_highs.values)]four_annotations[:1]{'x': Timestamp('2018-02-05 06:30:00'), 'y': 17.98865890412107, 'xref': 'x', 'yref': 'y', 'font': {'color': 'blue'}, 'text': 'Mon <br> 06:30 AM<br> 18.0 Mlbs/hr'}}(The <br> in the text is html which is read by plotly for displaying).There are other parameters of an annotation that we can modify, but we’ll let plotly take care of the details..Adding the annotations to the plot is as simple as passing them to the layout :layout = go.Layout(height=800, width=1000, title='Steam Sensor with Daily High Annotations', annotations=four_annotations)After a little post-processing in the online editor, our final plot is:Final plot with steam sensor annotationsThe extra annotations can give us insights into our data by showing when the daily peak in steam usage occurs..In turn, this will allow us to make steam start time recommendations to building engineers.The best part about plotly is we can get a basic plot quickly and extend the capabilities with a little more code..The upfront investment for a basic plot pays off when we want to add increased functionality.ConclusionsWe have only scratched the surface of what we can do in plotly..I’ll explore some of these functions in a future article, and refer to the notebook for how to add even more interactivity such as selection menus.. More details

Leave a Reply