Thanks to theidioms.com

Learn Matplotlib for Data Science (Course III)

Learn Matplotlib for Data Science (Course III)

Plotting 2D plots in Matplotlib

Now that you have learned the basics of a Matplotlib plot, in this chapter, we will be exploring the different kinds of 2D plots in Matplotlib.

A 2D plot is a plot where data is plotted on only the x and y-axis. 2D plots are mostly used in reporting and infographics and it is important to know how to plot such Matplotlib plots if you are a numerical analyst. The different types of 2D plots covered in this chapter are:

Matplotlib Line Plot – How to make a line plot in Matplotlib?

A Matplotlib Line Plot can be made using the plot() function of Matplotlib pyplot.

For plotting a Matplotlib Line Plot, we will have to specify the data for the x-axis and y-axis as shown in the example below:

# Libraries/Modules import conventions
import matplotlib.pyplot as plt 
%matplotlib inline 

# Dummy Data
X = [1, 2, 3, 4]
Y = [2, 4, 6, 8]

# plot() is used for plotting a line plot
plt.plot(X, Y)

# Adding title, xlabel and ylabel
plt.title('A Basic Line Plot') # Title of the plot
plt.xlabel('X-axis') # X-Label
plt.ylabel('Y-axis') # Y-Label

# show() is used for displaying the plot
plt.show()
2d plots in matplotlib

To learn more about the different variations of a line plot, please make sure to read the chapter on ‘Basics of a Matplotlib Plot‘.

Matplotlib Scatter Plot – How to make a scatter plot in Matplotlib?

A Matplotlib Scatter Plot can be made using the scatter() function of Matplotlib pyplot.

For plotting a Matplotlib Scatter Plot, we will have to specify the data for the x-axis and y-axis as shown in the example below:

# Libraries/Modules import conventions
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline 

# Making the random number generator always generate the same random numbers
np.random.seed(10)

# Dummy Data
x = np.arange(50)
y = x + 10 * np.random.randn(50)

# scatter() is used for plotting a scatter plot
plt.scatter(x, y)

# Adding title, xlabel and ylabel
plt.title('A Basic Scatter Plot') # Title of the plot
plt.xlabel('X-axis') # X-Label
plt.ylabel('Y-axis') # Y-Label

# show() is used for displaying the plot
plt.show()
matplotlib scatter plot

Adding size and color to a Matplotlib Scatter Plot

The scatter() function also allows us to define the size and color of each point being plotted. For this, we need to provide a list/array that contains the size and color of each point in the scatter() function.

# Libraries/Modules import conventions
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline 

# Making the random number generator always generate the same random numbers
np.random.seed(10)

# Dummy Data
x = np.arange(50)
y = x + 10 * np.random.randn(50)

# Defining sizes and colors
sizes = np.abs(np.random.randn(50)) * 100
colors = np.random.randint(0, 50, 50)

# scatter() is used for plotting a scatter plot
plt.scatter(x, y, s=sizes, c=colors)

# Adding title, xlabel and ylabel
plt.title('A Colorful Scatter Plot') # Title of the plot
plt.xlabel('X-axis') # X-Label
plt.ylabel('Y-axis') # Y-Label

# show() is used for displaying the plot
plt.show()
matplotlib colored scatter plot

Plotting your own data on a Matplotlib Scatter Plot

The above examples plotted data that were randomly generated to show you how to plot a scatter plot. Now, let us see how you can create your own lists and plot it as a scatter plot in Matplotlib.

# Libraries/Modules import conventions
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline 

# Making the random number generator always generate the same random numbers
np.random.seed(10)

# Dummy Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Defining sizes and colors
sizes = [112, 380, 100, 12, 60]
colors = [4, 20, 11, 3, 1]

# scatter() is used for plotting a scatter plot
plt.scatter(x, y, s=sizes, c=colors)

# Adding title, xlabel and ylabel
plt.title('A Colorful Scatter Plot') # Title of the plot
plt.xlabel('X-axis') # X-Label
plt.ylabel('Y-axis') # Y-Label

# show() is used for displaying the plot
plt.show()
colored scatter plot matplotlib

Matplotlib Bar Plot – How to make a bar plot in Matplotlib?

A Matplotlib Bar Plot can be made using the bar() and barh() functions of Matplotlib pyplot.

The bar() function is used to create a vertical Matplotlib Bar Plot and the barh() function is used to create a horizontal Matplotlib Bar Plot.

Plotting a vertical Matplotlib Bar Plot

A vertical Matplotlib Bar Plot can be made using the bar() function of Matplotlib pyplot.

For plotting a vertical Matplotlib Bar Plot, we will have to specify the data for the x-axis and y-axis as shown in the example below:

# Libraries/Modules import conventions
import matplotlib.pyplot as plt
%matplotlib inline 

# Dummy Data
x = ['Year 1', 'Year 2', 'Year 3', 'Year 4','Year 5']
y = [235, 554, 582, 695, 545]

# bar() is used for plotting a vertical bar plot
plt.bar(x, y)

# Adding title, xlabel and ylabel
plt.title('A Vertical Bar Plot') # Title of the plot
plt.xlabel('X-axis') # X-Label
plt.ylabel('Y-axis') # Y-Label

# show() is used for displaying the plot
plt.show()
matplotlib vertical bar plot

Plotting a stacked vertical Matplotlib Bar Plot

A stacked vertical Matplotlib Bar Plot can be plotted by plotting more than one vertical bar plot in the same Matplotlib figure.

The following example shows a stacked vertical Matplotlib Bar Plot:

# Libraries/Modules import conventions
import matplotlib.pyplot as plt
%matplotlib inline 

# Dummy Data
x = ['Year 1', 'Year 2', 'Year 3', 'Year 4','Year 5']
y1 = [235, 554, 582, 695, 545]
y2 = [100, 200, 500, 600, 800]
width = 0.35  # the width of the bars

# Making the plot for y1 list's data and plotting
p1 = plt.bar(x, y1, width)

# Stacking the y2 list's data at top and plotting
p2 = plt.bar(x, y2, width, bottom = y1)

# legend() is used for displaying the plot legend
plt.legend(['y1','y2'])

# Adding title, xlabel and ylabel
plt.title('A Stacked Vertical Bar Plot') # Title of the plot
plt.xlabel('X-axis') # X-Label
plt.ylabel('Y-axis') # Y-Label

# show() is used for displaying the plot
plt.show()
matplotlib stacked vertical bar plot

Plotting a horizontal Matplotlib Bar Plot

A horizontal Matplotlib Bar Plot can be made using the barh() function of Matplotlib pyplot.

For plotting a horizontal Matplotlib Bar Plot, we will have to specify the data for the x-axis and y-axis as shown in the example below:

# Libraries/Modules import conventions
import matplotlib.pyplot as plt
%matplotlib inline 

# Dummy Data
x = ['Year 1', 'Year 2', 'Year 3', 'Year 4','Year 5']
y = [235, 554, 582, 695, 545]

# bar() is used for plotting a vertical bar plot
plt.barh(x, y)

# Adding title, xlabel and ylabel
plt.title('A Horizontal Bar Plot') # Title of the plot
plt.xlabel('X-axis') # X-Label
plt.ylabel('Y-axis') # Y-Label

# show() is used for displaying the plot
plt.show()
horizontal bar plot

Plotting a stacked horizontal Matplotlib Bar Plot

A stacked horizontal Matplotlib Bar Plot can be plotted by plotting more than one horizontal bar plot in the same Matplotlib figure.

The following example shows a stacked horizontal Matplotlib Bar Plot:

# Libraries/Modules import conventions
import matplotlib.pyplot as plt
%matplotlib inline 

# Dummy Data
x = ['Year 1', 'Year 2', 'Year 3', 'Year 4','Year 5']
y1 = [235, 554, 582, 695, 545]
y2 = [100, 200, 500, 600, 800]
width = 0.35  # the width of the bars

# Making the plot for y1 list's data and plotting
p1 = plt.barh(x, y1, width)

# Stacking the y2 list's data at top and plotting
p2 = plt.barh(x, y2, width, left = y1)

# legend() is used for displaying the plot legend
plt.legend(['y1','y2'])

# Adding title, xlabel and ylabel
plt.title('A Stacked Horizontal Bar Plot') # Title of the plot
plt.xlabel('X-axis') # X-Label
plt.ylabel('Y-axis') # Y-Label

# show() is used for displaying the plot
plt.show()
stacked horizontal bar plot matplotlib

Matplotlib Pie Plot – How to make a pie chart in Matplotlib?

A Matplotlib Pie Plot can be made using the pie() function of Matplotlib pyplot.

For plotting a horizontal Matplotlib Pie Plot, we will have to specify the data as well as the label associated with it as shown below:

# Libraries/Modules import conventions
import matplotlib.pyplot as plt
%matplotlib inline 

# Dummy data
label = ['Year 1', 'Year 2', 'Year 3', 'Year 4','Year 5']
values = [235, 695, 554, 550, 545]

# pie() is used for plotting a pie chart
plt.pie(values, labels = label, startangle = 45)

# show() is used for displaying the plot
plt.show()
matplotlib pie plot

Exploding a pie out of the Matplotlib Pie Chart

Whenever we need to highlight important information about a certain pie, we can use the ‘explode’ parameter of a Matplotlib Pie Chart.

# Libraries/Modules import conventions
import matplotlib.pyplot as plt
%matplotlib inline 

# Dummy data
label = ['Year 1', 'Year 2', 'Year 3', 'Year 4','Year 5']
values = [235, 695, 554, 550, 545]

# Defining a pie that is to explode outside of the pie chart
Explode = (0, 0.1, 0, 0,0)  # only "explode" the 2nd slice (i.e. 'Year 2')

# pie() is used for plotting a pie chart and 'explode' property is used to explode a pie
plt.pie(values, labels = label, explode = Explode, startangle = 45)

# show() is used for displaying the plot
plt.show()
matplotlib exploding pie plot

Matplotlib Histogram Plot – How to make a histogram in Matplotlib?

A Matplotlib Histogram Plot can be made using the hist() function of Matplotlib pyplot.

For plotting a Matplotlib Histogram Plot, we will have to specify the data for the x-axis and y-axis as shown in the example below:

# Libraries/Modules import conventions
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline 

# Making the random number generator always generate the same random numbers
np.random.seed(10)

# Preparing random data
x = np.random.normal(size = 1000)

# hist() is used for plotting a histogram
plt.hist(x, density = True, bins = 30)

# Adding title, xlabel and ylabel
plt.title('A Histogram Plot') # Title of the plot
plt.xlabel('X-axis') # X-Label
plt.ylabel('Y-axis') # Y-Label

# show() is used for displaying the plot
plt.show()
matplotlib histogram plot

In this chapter, we learned to plot the following 2D plots: Matplotlib Line Plot, Matplotlib Scatter Plot, Matplotlib Bar Plot, Matplotlib Pie Plot and Matplotlib Histogram Plot.

In the next chapter, we will learn how to plot 3D plots in Matplotlib. Head over to the next chapter and learn about the different available 3D plots in Matplotlib.

Leave your thought here

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami