#241 Improve area chart

The chart #240 describes how to make a basic area chart. This page aims to describe a few customisation you can apply to your area chart. It is highly advised to change the default colour of matplotlib. Moreover, it is quite common to use a color with transparency, and add a line with a stronger colour on the top of the area. These 2 elements are drawn using 2 lines of code, one for the area (fill_between), and one for the line (plot).


# library
import numpy as np
import matplotlib.pyplot as plt

# create data
x=range(1,15)
y=[1,4,6,8,4,5,3,2,4,1,5,6,8,7]

# Change the color and its transparency
plt.fill_between( x, y, color="skyblue", alpha=0.4)
plt.show()

# Same, but add a stronger line on top (edge)
plt.fill_between( x, y, color="skyblue", alpha=0.2)
plt.plot(x, y, color="Slateblue", alpha=0.6)
# See the line plot function to learn how to customize the plt.plot function


Last but not least, note that it is often worth it to call the seaborn library before doing your chart. It allows you to benefit its nice looking style. Moreover, don’t forget to add a title and give names to axis:


# get seaborn
import seaborn as sns

# Make the same graph
plt.fill_between( x, y, color="skyblue", alpha=0.3)
plt.plot(x, y, color="skyblue")

# Add titles
plt.title("An area chart", loc="left")
plt.xlabel("Value of X")
plt.ylabel("Value of Y")

Leave a Reply

Your email address will not be published.