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")
Last updated