Comparisons
Comparisons are fundamental to data visualization as they enable viewers to understand relationships, differences, and trends within the data, ultimately uncovering the key takeaways of a chart. Whether comparing categories, time periods, or variables, visualizations such as bar charts, line graphs, or scatter plots highlight how values differ or correlate, providing context and meaning. For instance, a bar chart comparing sales across regions reveals top-performing areas, while a line chart showing revenue over time identifies growth or decline. Without clear comparisons, the data may appear isolated and lack actionable insights. By presenting data in a way that facilitates side-by-side evaluations, comparisons make it easier to identify patterns, spot outliers, and prioritize areas of focus, ensuring that the visualization effectively communicates its intended message.
Use the add_trace
to add lines from a comparison stock on the line chart.
// Some code
import plotly.express as px
import pandas as pd
import yfinance as yf
#download boeing data for one year; interval = '1mo' for monthly data
boeing = yf.download('BA', period='1y', interval='1d')
boeing.reset_index(inplace=True)
boeing.columns = boeing.columns.to_flat_index()
boeing.columns = ['_'.join(col) for col in boeing.columns]
boeing.columns = ["Date", "Close", "High", "Low", "Open", "Volume"] # rename columns
# download Airbus data
airbus = yf.download('EADSY', period='1y', interval='1d')
airbus.reset_index(inplace=True)
airbus.columns = airbus.columns.to_flat_index()
airbus.columns = ['_'.join(col) for col in airbus.columns]
airbus.columns = ["Date", "Close_Airbus", "High_Airbus",
"Low_Airbus", "Open_Airbus", "Volume_Airbus"] # rename columns
fig = px.line(boeing, x="Date", y="Close_BA", color_discrete_sequence=["red"])
fig.add_trace(px.line(airbus, x="Date", y="Close_Airbus", color_discrete_sequence=["black"]).data[0])
To change the formatting, update the layout and axes
fig.update_layout(
title = 'Time Series with Custom Date-Time Format',
xaxis_tickformat = '%d %B (%a)<br>%Y',
xaxis_title = 'Date',
yaxis_title = 'Price'
)
fig.update_layout(yaxis_tickformat = '$.0f') #'%'
fig.update_layout(
plot_bgcolor='white'
)
fig.update_xaxes(
mirror=True,
ticks='outside',
showline=True,
linecolor='black',
gridcolor='lightgrey'
)
fig.update_yaxes(
mirror=True,
ticks='outside',
showline=True,
linecolor='black',
gridcolor='lightgrey'
)
fig.show()
Last updated