Python Matplotlib – X&Y plot types

In this post we will be generating different types of X&Y plot types in Python, to do this we will be making use of the Matplotlib library.

For the purpose of this post we will be plotting the same X&Y data each time, our X and Y data arrays can be seen below consisting of ten data points.

X = [1,2,3,4,5,6,7,8,9,10]
Y = [55,66,69,72,85,92,101,116,102,95]

Scatter Plot

The snippet of code below will generate a scatter plot using our X and Y data arrays.

import matplotlib.pyplot as plt

X = [1,2,3,4,5,6,7,8,9,10]
Y = [55,66,69,72,85,92,101,116,102,95]

plt.scatter(X, Y)
plt.show()

The output of the above is seen as.

Scatter plot example

Bar Plot

The snippet of code below will generate a bar plot using our X and Y data arrays.

import matplotlib.pyplot as plt

X = [1,2,3,4,5,6,7,8,9,10]
Y = [55,66,69,72,85,92,101,116,102,95]

plt.bar(X, Y)
plt.show()

The output of the above is seen as.

Bar plot example

Stem Plot

The snippet of code below will generate a stem plot using our X and Y data arrays.

import matplotlib.pyplot as plt

X = [1,2,3,4,5,6,7,8,9,10]
Y = [55,66,69,72,85,92,101,116,102,95]

plt.stem(X, Y)
plt.show()

The output of the above is seen as.

Stem plot example

Step Plot

The snippet of code below will generate a step plot using our X and Y data arrays.

import matplotlib.pyplot as plt

X = [1,2,3,4,5,6,7,8,9,10]
Y = [55,66,69,72,85,92,101,116,102,95]

plt.step(X, Y)
plt.show()

The output of the above is seen as.

Step plot example

Stack Plot

For the stack plot we have had to introduce additional layers to our Y axis as seen below.

Y1 = [55,66,69,72,85,92,101,116,102,95]
Y2 = [42,54,62,69,72,75,83,87,91,98]
Y3 = [21,31,35,45,47,52,64,72,82,90]
Y4 = [2,6,8,7,5,9,11,16,14,19]
Y = np.vstack([Y1,Y2,Y3,Y4])

The snippet of code below will generate a step plot using our X and Y data arrays.

import matplotlib.pyplot as plt
import numpy as np

X = [1,2,3,4,5,6,7,8,9,10]
Y1 = [55,66,69,72,85,92,101,116,102,95]
Y2 = [42,54,62,69,72,75,83,87,91,98]
Y3 = [21,31,35,45,47,52,64,72,82,90]
Y4 = [2,6,8,7,5,9,11,16,14,19]
Y = np.vstack([Y1,Y2,Y3,Y4])

plt.stackplot(X, Y)
plt.show()

The output of the above is seen as.

Stack plot example

Leave a Reply