Making Numbers Pretty in MatPlotLib

Friday, September 23, 2022

Python is an incredible tool for crunching numbers, forcasting things, and cleaning up that data to look presentable. On a basic level, python has many libraries for analyzing data, but it is up to the programmer to take that data and present it in a way that illustrates the data easily.

In this post, I will show how I imported raw data with python’s MatPlotLib and formatted it to have a more branded look to match my website’s color pallette.

A Basic Chart: No Formatting

The first chart I created is a comparison of Tesla and Apple stock, which I overlayed with the S&P 500 to compare the growth of the two companies compared to that of the overall market.

The data is pulled from Yahoo’s Financial API using the pandas framework, so nothing too special. I simply set an array of tickers I wanted and called the get API.

After I did this, I plotted both Tesla and Apple on the same Y-axis scale, with the price being the Y-axis. As you can see in the image to the left, the graph gets the point across, but looks a bit amateurish.

Adding A Stylesheet

To fix this, I enlisted the help of MatPlotLib’s build-in stylesheets. These stylesheets are similar to a website’s CSS that are used to format and style text, graphics, and colors across the entire page. Here’s the beginning of mine:

# Set custom colors. All colors are in web style hex format.
axes.prop_cycle: cycler('color', ['006058', '57929E', '758D998A', '6D7C847E','6D7C8468','8A92977B'])


# Style spines
axes.linewidth: 1               # Spine edge line width
axes.spines.top: False          # Display axis spines (True or False)
axes.spines.left: False         # We only want the bottom spines
axes.spines.right: False
axes.spines.bottom: True

# Set line styling for line plots
lines.linewidth: 3              # line width in points
lines.solid_capstyle: butt      # Makes a square ending of the line stopping at datapoint

# Grid style
axes.grid: true                 # display grid or not
axes.grid.axis: y               # which axis the grid should apply to          
grid.linewidth: 1               # in points
grid.color: A5A5A5            # grid color
axes.axisbelow: True            # Sets axis gridlines below lines and patches.
axes.facecolor: EFEEE7        # Sets the background face color

# Move tick labels to right side
ytick.labelleft: False          # draw tick labels on the left side
ytick.labelright: True          # draw tick labels on the right side
ytick.alignment: bottom         # alignment of yticks

Starting with this, I played around with the stylesheet until I was happy with it, and created a graph image out of MatPlotLib that looked more professional and added to the branding of my site.

To call this stylesheet, I used the following line of code:

import matplotlib.pyplot as plt
import matplotlib.ticker as mtick

plt.style.use('../matplotlib/_stylesheets/matplotlib-stylesheet-sullysbrain.mplstyle')

I continued to modify the graph with inline python code to enhance the overall look and feel.

fig, ax = plt.subplots()
tesla = data['TSLA']
apple = data['AAPL']
sp500 = data['^GSPC']

ax.plot(data['TSLA'], label='Tesla', alpha=0.8, linewidth=2)
ax.plot(data['AAPL'], label='Apple')

ax.set_title("Stock Prices")
ax.set_ylim(0)
ax.yaxis.set_label_position("right")
ax.yaxis.tick_right()

# Add title & subtitle
ax.text(x=.08, y=.86, 
        s="Tesla v. Apple v. S&P500", 
        transform=fig.transFigure, 
        ha='left', 
        fontsize=20, 
        alpha=.6)

plt.show()

Seeing The Results

I added a few more minor tweaks after this to include the copyright and data source attribution, and the reslulting graph from python’s plot() command resulted in the following design.

By the way, I’m working on another post in preparation for Tesla’s AI Day next week. Over the past year, I have been tracking Tesla’s developments in chip design, AI research, and what it will look like as a company in the coming years. My upcoming post will highlight this analysis. Hint: I’m very bullish on Tesla’s future.

Another macro that I am following is the growth and development that China has had in the technology industry and what implications this may have. As a teaser in this research, I am tracking the GDP of several countries to analyze trends. I think this chart that I created makes one trend very obvious. The United States is still the king economy. But China is quickly catching up.

Many predictions from just ten or fifteen years ago have fallen short, calling for China to overtake the United States by as early as 2015. Needless to say, this never happened. One factor was the global shutdown. There have been other developments. Former President Trump had banned companies like Google, Intel, and Qualquamm from selling chips to China. And recently, the US banned chipmakers from selling their top AI chips to China and Russia. Again, this is a post for another day, but something I am following very closely.

Wrapping Up

If you are using MatPlotLib or any other library for illustrating data, I would highly recommend how that data is presented. Take another look at the default graph at the top of this post and look at how much more impact the branded ones have.

Data is important. Presenting data in a compelling way can be just as important.




Hi. I'm Scott Sullivan, a slave of Christ and husband to my awesome wife Angie. I'm an artist and writer, living in the beautiful countryside of Lancaster, Pa.

I recently published, Searching For Me, my first memoir. It’s about my adoption, search for my biological family, and how it affected my faith. Read about it here.