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

Polishing the visualization

In order to create a visualization that is appropriate for presentation, you need to polish the visualization and change the axis titles, background, labels, etc.

import pandas as pd
import yfinance as yf
import kaleido
import plotly.express as px
import plotly.io as pio

# Download the daily Boeing stock prices
start = '2024-01-01'
end = '2024-12-31'
boeing = yf.download('BA', start=start, end=end, 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

# Create the line chart
# To change the color of the line, use the color_discrete_sequence=["red"]
fig = px.line(boeing, x="Date", y="Close", color_discrete_sequence=["black"]) 

# Use the update_layout() function to change the title for the plot and for the axes
fig.update_layout(
    title = 'Boeing Stock Price: 2024 (Bar Chart)',
 #   xaxis_tickformat = '%d %B (%a)<br>%Y',
    xaxis_title = 'Date',
    yaxis_title = 'Price'
)

# Change the format of the y-axis to be currency
fig.update_layout(yaxis_tickformat = '$.0f') #'%'

# Change the format of the background plot
fig.update_layout(
    plot_bgcolor='white'
)

# Change the gridlines and x-axis tick marks
fig.update_xaxes(
    mirror=True, # showing the top line
    ticks='outside', # axis tick marks
    showline=True, # show the intercept lines
    linecolor='black', # the color of the intercept lines
    gridcolor='lightgrey' # the color of the gridlines
)

# Change the gridlines and y-axis tick marks
fig.update_yaxes(
    mirror=True, # showing the right, mirror line of the intercept
    ticks='outside',
    showline=True,
    linecolor='black',
    gridcolor='lightgrey'
)
fig.show()

# Save the image
pio.write_image(fig, "boeing_daily_line_polished.png", engine="kaleido")
PreviousUnderstanding Boeing Data Over TimeNextAnalyzing with AI

Last updated 3 months ago