Tuesday, December 24, 2019

Fundamentals of Matplotlib

Lets look at some of the important concepts that you shall come across and use in Matplotlib, and their meanings or roles:
  • Axis – This represents a number line, and is used to determine the graph limits.
  • Axes – These represent what we construe as plots. A single figure can hold as many axes as  possible. In the event of a 3D object, you can have two or three objects. Take note that for all axes, you must have an x and y label.
  • Artist – Refers to everything that you can see on your figure, for example collection objects, Line2D objects and Text objects. You will notice that most of the Artists are on the Axes.
  • Figure – Refers to the entire figure you are working on. It might include more than one plots or axes.
Pyplot is a Matplotlib module that allows you to work with simple functions, in the process adding elements like text, images, and lines within the figure you are working on. A simple plot can be created in the following manner:

import matplotlib.pyplot as plt
import numpy as np


There are lots of command functions that you can use to help you work with Matplotlib. Each of these pyplot functions changes figures in one way or the other when executed. The following is a list of the plots you will use in Matplotlib:

● Quiver – Used to create 2D arrow fields
● Step – Used to create a step plot
● Stem – Used to build a stem plot
● Scatter – Creates a scatter plot of x against y
● Stackplot – Used to create a plot for a stacked area
● Plot – Creates markers or plot lines to your axes
● Polar – Creates a polar plot
● Pie – Creates a pie chart
● Barh - Creates a horizontal bar plot
● Bar – Creates a bar plot
● Boxplot – Creates a whisker and box plot
● Hist – Used to create a histogram
● Hist2d – Used to create a histogram plot in 2D

Given that you might be working with images from time to time during data analysis, you will frequently use the following image functions:

● Imshow – Used to show images on your axes
● Imsave – Used to save arrays in the form of an image file
● Imread – Used to read files from images into arrays

Now it's time to create a plot but you must first import the Pyplot module from your Matplotlib package before you can create a plot. This is done as shown below:

import matplotlib.pyplot as plt

After importing the module, you introduce arrays into the plot. The NumPy library has predefined array functions that you will use going forward. These are imported as follows:

import numpy as np

With this done, proceed to introduce objects into the plot using the NumPy library’s arange() function as shown below:

x = np.arange(0, math.pi*2, 0.05)

With this data, you can then proceed to specify the x and y axis labels, and the plot title as shown:

plt.xlabel("angle")
plt.ylabel("sine")
plt.title('sine wave')


To view the window, use the show() function below:

plt.show()

At this juncture, your program should look like this:

from matplotlib import pyplot as plt
import numpy as np
import math #will help in defining pi
x = np.arange(0, math.pi*2, 0.05)
y = np.sin(x)
plt.plot(x,y)
plt.xlabel("angle")
plt.ylabel("sine")
plt.title('sine wave')
plt.show()


Before you plot on matplotlib, you must have a plot () function within the matplotlib.pyplot subpackage. This is to give you the basic plot with x-axis and y-axis variables. Now run the program and see the plot obtained. It should be like:


Share:

1 comment: