Intro to Data Visualization
  • Introduction
  • Getting started
    • Introduction to Pandas
    • Accessing Files on Colab
    • Reviewing Data
      • Understanding type(data) in Pandas
    • Data Types
      • Categorical Data
      • Numeric Data
      • Temporal Data
      • Geographic Data
    • How to Check Data Type
    • Slicing and Subsetting DataFrames
    • Aggregating Data
  • Visualization Types
    • Exploratory Process
    • Explanatory Process
  • data exploration
    • Exploration Overview
    • Exploration with Plotly
      • Exploring Distributions
      • Exploring Relationships
      • Exploring with Regression Plots
      • Exploring Correlations
      • Exploring Categories
      • Exploring Time Series
      • Exploring Stocks with Candlestick
      • Exploring with Facets
      • Exploring with Subplots
    • Exploring with AI
  • Data Explanation
    • Data Explanation with Plotly
      • Using Text
      • Using Annotations
      • Using Color
      • Using Shape
      • Accessibility
      • Using Animations
    • Use Cases
  • Exercises and examples
    • Stock Market
      • Loading Yahoo! Finance Data
      • Use Cases for YF
      • Exploring YF Data
      • Understanding Boeing Data Over Time
      • Polishing the visualization
      • Analyzing with AI
      • Comparisons
    • The Gapminder Dataset
      • Loading the Gapminder Data
      • Use Cases
      • Exploring the Data
      • Exporting a Static Image
Powered by GitBook
On this page
  1. Exercises and examples
  2. Stock Market

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()
PreviousAnalyzing with AINextThe Gapminder Dataset

Last updated 3 months ago