# Python Matplotlib

Filed Under: Python

Python matplotlib library helps us to plot data on graphs in its simplest terms. If you are familiar with MATLAB plotting, then Matplotlib will be easy to use for basic plotting.

## Python Matplotlib

To start understanding how Matplotlib helps us building graphs and visualisation figures to represent data, we will need to know some of the basic terms we will use a lot in this post. Let’s study these terms first.

### Python Matplotlib Terminology

• The Figure is complete window or the page the graph is drawn upon.
• The Axes is the area on which data is plotted. This can be X-Axis or Y-Axis etc.
• The Spines are the lines which connects Axes points.

### Install Matplotlib

It is easy to install python matplotlib library with `pip`:

``````
pip install matplotlib
``````

That’s it! Now we are ready to build some cool examples using this data visualisation library.

## Getting started with Matplotlib

In this section, we will get started with the plot construction and start feeding data to python matplotlib functions.

### Matplotlib Line Plot

We will start with a very basic example of plotting. We will just use two Python lists as the data source for points of a graph. Let’s write a code snippet for this:

``````
import matplotlib.pyplot as plt

year = [1950, 1975, 2000, 2018]
population = [2.12, 3.681, 5.312, 6.981]

plt.plot(year, population)
plt.show()
``````

Note the last line with the `show()` function. It is important to call it otherwise, the plot won’t be shown on the screen. When we run this code, we can see the following figure appear: Note that we can also provide a title to this figure and labels to our Axes by using this snippet:

``````
...
plt.xlabel('Year')
plt.ylabel('Population')
plt.title('World Population')
``````

### Matplotlib Scatter Plot

Above plot was very much indicative of the points which were actually not passed in the array as it showed a line. What if we only want to see the actual points on the plot? Scatter plot achieves this:

``````
import matplotlib.pyplot as plt

year = [1950, 1975, 2000, 2018]
population = [2.12, 3.681, 5.312, 6.981]

plt.scatter(year, population)
plt.show()
``````

When we run this code, we can see the following figure appear: ### Matplotlib Historgrams

In this section, we introduce you to Histograms. While graphs inform us how our data vary, histogram describes how our data is distributed. More the values in a range, higher the bar for a range.

We use hist() function to make a Histogram. It has 2 important parameters:

• List of values to plot
• Number of ranges to distribute these points into

Let’s demonstrate this with a code snippet:

``````
import matplotlib.pyplot as plt

values = [0, 1.2, 1.3, 1.9, 4.3, 2.5, 2.7, 4.3, 1.3, 3.9]
plt.hist(values, bins = 4)
plt.show()
``````

When we run this code, we can see the following figure appear: The default value for number of bins is 10. The number of bins is important to set. Smaller number of bins can hide reality of data distribution and too many bins can overcomplicate reality.

### Customisation in Matplotlib Plot

If you notice the first Line plot, we see that Y-axis didn’t started from 0. We can modify this:

``````
...
plt.yticks([0, 2, 4, 6, 8, 10])
``````

When we run this code, we can see the following figure appear: ### Drawing multiple curves in Matplotlib

It is utterly common to draw multiple curves on a single graph to make a comparison. Let’s try this here:

``````
import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
cos, sin = np.cos(X), np.sin(X)

plt.plot(X, cos)
plt.plot(X, sin)

plt.show()
``````

When we run this code, we can see the following figure appear: So, it was just a matter of calling plot multiple times. To add, we used numpy to create a non-linear curve!

### Changing color and Adding Legends in Matplotlib Plot

As we saw, curves look nice but aren’t they all look so, similar? What if we want to change their color and show what each color represents? Let’s try drawing the sine and cosine curves together:

``````
import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
cos, sin = np.cos(X), np.sin(X)

plt.plot(X, cos, color='blue', label="cosine")
plt.plot(X, sin, color='red', label="sine")
plt.legend(loc='upper left', frameon=False)

plt.show()
``````

When we run this code, we can see the following figure appear: If you notice, we actually did two things in this figure:

1. Modified the color for the curves to make the comparison easier
2. Added a legend frame which introduces which colour represents what. This makes the metadata on the graph very easy to read.

### Creating Bar chart in Matplotlib

We can create appealing bar charts with Matplotlib with simple code snippet:

``````
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

names = ('Tom', 'Dick', 'Harry', 'Jill', 'Meredith', 'George')
y_pos = np.arange(len(names))
speed = [8, 7, 12, 4, 3, 2]

plt.bar(y_pos, speed, align='center', alpha=0.5)
plt.xticks(y_pos, names)
plt.ylabel('Speed')
plt.title('Person')

plt.show()
``````

When we run this code, we can see the following figure appear: ### Creating Pie chart in Matplotlib

We can create appealing pie charts with Matplotlib with simple code snippet:

``````
import matplotlib.pyplot as plt

# Data to plot
names = 'Tom', 'Dick', 'Harry', 'Jill', 'Meredith', 'George'
speed = [8, 7, 12, 4, 3, 2]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'red', 'blue']
explode = (0.1, 0, 0, 0, 0, 0)  # explode 1st slice

# Plot
plt.pie(speed, explode=explode, labels=names, colors=colors,

plt.axis('equal')
plt.show()
``````

When we run this code, we can see the following figure appear: See how we elevated one of the slice in the pie chart to differentiate it from the rest!

### Creating Heat Maps in Matplotlib

Charts are cool but when it comes to visualising geographical information, nothing works better than a heat map:

``````
import numpy as np
import numpy.random
import matplotlib.pyplot as plt

# Create data
temperature = np.random.randn(4096)
anger = np.random.randn(4096)

# Create heatmap
heatmap, xedges, yedges = np.histogram2d(temperature, anger, bins=(64,64))
extent = [xedges, xedges[-1], yedges, yedges[-1]]

# Plot heatmap
plt.clf()
plt.ylabel('Anger')
plt.xlabel('Temp')
plt.imshow(heatmap, extent=extent)
plt.show()
``````

When we run this code, we can see the following figure appear: Note that we created the data by just random values and output figure can vary depending on the values.

That’s all for python matplotlib plotting tutorial.

Reference: Website

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages