What is Area Plot?
Area charts based on line chart having part below line filled with color. An area graph represents change in quantities of one or more groups over time. Area plot are extension of line graph, it display colorful and shows solid part of chart to represent values clear and easily with different shades between the lines.
We can plot area chart using fill_between()
function of matplotlib and using stackplot()
function of matplotlib.
In this article, I will explain using fill_between() and stackplot() function to plot area chart in python using matplotlib package.
Using Parameters for Customizing Area Chart
Parameters using matplotlib.pyplot to customize area plot are given below
Parameters ---------- x : 1d array of dimension N y : 2d array (dimension MxN), or sequence of 1d arrays (each dimension 1xN) The data is assumed to be unstacked. Each of the following calls is legal:: stackplot(x, y) # where y is MxN stackplot(x, y1, y2, y3, y4) # where y1, y2, y3, y4, are all 1xNm baseline : {'zero', 'sym', 'wiggle', 'weighted_wiggle'} Method used to calculate the baseline: - ``'zero'``: Constant zero baseline, i.e. a simple stacked plot. - ``'sym'``: Symmetric around zero and is sometimes called 'ThemeRiver'. - ``'wiggle'``: Minimizes the sum of the squared slopes. - ``'weighted_wiggle'``: Does the same but weights to account for size of each layer. It is also called 'Streamgraph'-layout. More details can be found at http://leebyron.com/streamgraph/. labels : Length N sequence of strings Labels to assign to each data series. colors : Length N sequence of colors A list or tuple of colors. These will be cycled through and used to colour the stacked areas. **kwargs All other keyword arguments are passed to `Axes.fill_between()`. Parameters using pandas to customize area plot Parameters ---------- x : label or position, optional Coordinates for the X axis. By default uses the index. y : label or position, optional Column to plot. By default uses all columns. stacked : bool, default True Area plots are stacked by default. Set to False to create a unstacked plot. **kwargs Additional keyword arguments are documented in :meth:`DataFrame.plot`
Installation of Packages
We will need numpy and matplotlib packages to plot area chart in python. If you don’t have these packages installed on your system, install it using below commands.
pip install numpy pip install matplotlib
Cool Tip: Learn how to plot basic stacked area chart in python !
How to Plot Area Chart in Python
Let’s see an example to plot basic area chart using matplotlib fill_between
function.
Installation of Packages
We will need numpy and matplotlib packages to plot area chart. Install packages using below command.
pip install numpy pip install matplotlib
Import libraries
Import numpy and matplotlib libraries in our python code to get started with plotting area graph.
import numpy as np import matplotlib.pyplot as plt
Prepare dataset
We will need x and y axis dataset values to plot area chart. Lets create dataset for x and y using Numpy arange() to get array object evenly spaces values within given range
# Create dataset x=np.arange(1,10) y=[1,6,4,9,6,5,10,8,4]
Plot Area chart using fill_between()
using matplotlib fill_between() function which accepts x and y values to plot area chart
plt.fill_between(x, y)
Show Area Chart
Using Matplotlib show() function to show the graphical view of area chart.
plt.show()
Area plot Python Code
Use below entire area plot in python using Matplotlib source code
# Import libraries import numpy as np import matplotlib.pyplot as plt # Create data x=np.arange(1,10) y=[1,6,4,9,6,5,10,8,4] # Area plot plt.fill_between(x, y) plt.show()
Basic Fill Area Plot Output
Area plot in Python using Matplotlib
Let’s see an example to plot basic area chart using matplotlib stackplot()
function.
Installation of Packages
We will require numpy and matplotlib packages installed on our system to plot area plot. If we don’t have packages installed, we can install packages with the command below.
pip install numpy pip install matplotlib
Import libraries
We will import numpy and matplotlib libraries to plot area plot. we have imported the numpy library as np and matplotlib.pyplot library as plt to reduce the complexity of code.
import numpy as np import matplotlib.pyplot as plt
Prepare dataset
We have created variables x and y as datasets to plot area plot using matplotlib stackplot() function. Lets create dataset for x and y using Numpy arange() to get array object evenly spaces values within given range
# Create dataset x=np.arange(1,10) y=np.arange(1,10)
Plot Area chart
Using matplotlib stackplot() function which accepts x and y values to plot area chart
plt.stackplot(x,y)
Show Area Chart
Using Matplotlib show() function to show the graphical view of area chart.
plt.show()
Area plot Python Code
Use below entire area plot in python using Matplotlib source code
# Import libraries import numpy as np import matplotlib.pyplot as plt # Create dataset x=np.arange(1,10) y=np.arange(1,10) # Area plot plt.stackplot(x,y) plt.show()
Area Chart output
Conclusion
I hope you found above article on basic area plot in python using matplotlib package informative and educational.