0% found this document useful (0 votes)
13 views

5 Plotting With Matplotlib

Uploaded by

AB
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

5 Plotting With Matplotlib

Uploaded by

AB
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

7/19/24, 9:57 AM 5-plotting-with-matplotlib.

ipynb - Colab

Why we need Data Visualization?!

In today's business world, so much information is gathered through data analysis that we need a way to paint a picture of that data so that we
can interpret it. Data visualization clearly shows what the information means by providing visual context through maps and graphs. This makes
data more understandable to the human mind, so you can easily spot trends, patterns, and outliers in large datasets.

Human perception itself was designed to assimilate figures and graphs more than numbers, even for those who are very obsessive with math.
To give an example, imagine sitting in front of a 30-million-row table of data and trying to notice anything insightful with your bare eyes! What
can be noticed?!

Now, reimagine this scenario, but with this gigantic table expressed in figures and graphs. Is it easier now for your bare eyes to catch something
insightful?

If your answer to the above question is yes, as it should be, then this can be a clear example of the importance of visualization in general and in
data analysis specifically.

What is matplotlib?
Matplotlib is a Python programming language library used for plotting and visualizing data. It is the most prominent of all Python visualization
packages and offers a wide range of functions. It can produce high-quality figures in various formats, such as PDF, SVG, JPG, PNG, BMP, and
GIF. Matplotlib also offers a variety of plot types including line plots, scatter plots, histograms, bar charts, error charts, pie charts, and box plots.

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 1/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab
It also supports 3D plotting and many other libraries are built on top of it, such as pandas and Seaborn, allowing access to Matplotlib's methods
with less code.

The open-source tool Matplotlib was started by John Hunter in 2002 for his post-doctoral research in Neurobiology to visualize
Electrocorticography (ECoG) data from epilepsy patients.

Why learning matplotlib can be overwhelming?


Learning matplotlib can be a frustrating operation. The problem is not the insufficiency of matplotlib documentation. The documentation is
comprehensive. However, it can cause some problems due to the following points:

The library itself is huge, with about 70,000 lines of code in total.
Matplotlib hosts several different interfaces (methods of creating characters) and can interact with several different backends. (The
backend handles the process of how the chart is rendered, not just the internals.)
Although comprehensive, some of matplotlib's public documentation is very outdated.

Anatomy of a figure in matplotlib

Matplotlib library is based on NumPy . This is a Python library used to visualize data. This is a tree-like hierarchy of objects that make up each
of these plots.

The figure in matplotlib can be understood as the outermost border of the graph. This figure can contain multiple axis objects.

Axes can be understood as part of a figure , a subplot in matplotlib terms. It can be used to manipulate any aspect of the charts it
contains. figure object in matplotlib is a box that contains one or more Axes objects. The following gaph is summerizing this.

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 2/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

Importing Matplotlib
Before using matplotlib, we need to import it. We can import Matplotlib as follows:

import matplotlib

Most of the time, we have to work with pyplot interface of matplotlib. So, we will import pyplot interface of matplotlib as follows:

import matplotlib.pyplot

To make things simpler, we will use standard shorthand for matplotlib imports as follows:

import matplotlib.pyplot as plt

Displaying Plots in matplotlib


The way in which we view the Matplotlib plot depends on our context. The optimal use of Matplotlib varies depending on the purpose for which
it is being used. There are three distinct contexts in which the plots can be seen: when plotting from a script, when plotting from an IPython
shell, and when plotting from a Jupyter notebook.

Plotting from a script


The plt.show() command is very useful when working with Matplotlib within a script, as it starts an event loop which looks for all active figure
objects and opens up interactive windows to display them. It should only be used once per Python session, and should be placed at the end of
the script. Using multiple plt.show() commands can cause unexpected results, so it is best to avoid doing this.

Plotting from an IPython shell


It is possible to use Matplotlib within an IPython shell, though this method is becoming less common. To enable this mode, the %matplotlib
magic command must be used after starting ipython. After this command is used, any plt plot command will open a figure window and
subsequent commands can be run to update the plot.

Plotting from a Jupyter notebook

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 3/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab
The Jupyter Notebook (formerly the IPython Notebook) is a comprehensive data analysis and visualization platform, offering multiple features
in one place. It allows users to execute code, create graphical plots, display rich text and media, write mathematical equations and much more -
all within a single executable document.

To enable interactive plotting within the notebook, the command %matplotlib should be used. There are two ways to work with graphics in
Jupyter Notebook.These are as follows:

%matplotlib notebook – This command will produce interactive plots embedded within the notebook.
%matplotlib inline – It will output static images of the plot embedded in the notebook.

After this command (it needs to be done only once per kernel per session), any cell within the notebook that creates a plot will embed a PNG
image of the graphic.

Funtions to different plots


In matplotlib, there are different funtions to different plots. Below is a list of those functions.
Type of Plot Function

Line Plot (Default) plt.plot()

Vertical Bar Chart plt.bar()

Horizontal Bar Chart plt.barh()

Error Bar Chart plt.errorbar()

Histogram plt.hist()

Box Plot plt.box()

Area Plot plt.area()

Scatter Plot plt.scatter()

Pie Plot plt.pie()

Hexagonal Bins Plot plt.hexbin()

keyboard_arrow_down Fundamental Plot


The default and basic plot function in matplotlib is plt.plot() .The syntax for line plot is as follows:

matplotlib.pyplot.plot(x, y, scalex=True, scaley=True, data=None, **kwargs)

Although the syntax is very simple but it has several interfaces and can be coded in different ways.

plot(x, y) # plot x and y using default line style and color


plot(x, y, 'bo') # plot x and y using blue circle markers
plot(y) # plot y using x as index array 0..N-1
plot(y, 'r+') # ditto, but with red plusses

I highly recommend reading the documentation

Lets see a very basic plot!

# import packages
import pandas as pd
import numpy as np
# setting random seed
np.random.seed(101)
# import matplotlib
import matplotlib.pyplot as plt

# create random array


rnd_arr = np.arange(0, 8, 0.01)
# plot function
plt.plot(rnd_arr, [i**3 for i in rnd_arr ], 'b--')
# show the plot
plt.show()

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 4/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

keyboard_arrow_down Multiline Plot


Multiline plots are used to show relationships between two or more variables. They are used to display trends over time, compare different
groups, and show the interactions between multiple variables. Multiline plots can be used to represent data with multiple lines, each of which
represents a different feature.

This can be easily by simply using multible plt.plot() functions before calling plt.show()

plt.plot(rnd_arr, [i*5 for i in rnd_arr], 'b-')


plt.plot(rnd_arr, [i**2 for i in rnd_arr], 'g-')
plt.plot(rnd_arr, [i/5 for i in rnd_arr], 'y-')
# show
plt.show()

keyboard_arrow_down Line Plot


A line plot uses line segments to connect data points. Line plots are one of the most commonly used types of plots. These are especially useful
in situations that show changes or other variables that occur over a period of time.

Advantages
Very powerful for representing continuous data, such as change over time.
Allows possible extrapolation of data.
Having a line constructed from multiple data points can allow you to make estimates of missing data.
Allows comparison of two or more features to see if there is any kind of connection or relationship.

Disadvantages
Can be challenging to determine exact values at a given point of the graph
Two lines that have values that are very similar, can make comparing data difficult.

In our example, we will follow the same methodology, create a figure, create axes, then the plot itself.In general, one of the most things you will
need to do with plots is to change the size of a plot. Lets see the syntax.

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 5/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab
# create rnd_x :: 1000 random point between 0 and 1
rnd_x = np.linspace(0, 10, 1000)
# create rnd_y :: cos value of each point in x
rnd_y = np.cos(rnd_x)
# plot
plt.plot(rnd_x, rnd_y, 'b-');

keyboard_arrow_down Bar Plot


Bar graphs are ideal for demonstrating segments of information, especially when comparing different categories or discrete variables. They are
particularly useful for comparing age groups, classes, schools and the like, as long as there are not too many to compare. Additionally, bar
graphs are well-suited for displaying time series data due to the limited space on the x-axis that is ideal for representing years, minutes, hours
or months.

Advantages
Visualizes large data sets.
Understandable to the majority
Shows relative numbers or proportions of multiple categories

Disadvantages
Easily manipulated to give wrong impressions.
Not suitable if there are huge numbers of categories.

Lets see the syntax of bar plot:

matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)

the arguments related to plt.bar() are very straightforward. However, I recommend reading the documentation

Now, lets do some example using plt.bar()

# create random list of 8 numbers


rnd_lst = list(np.random.randint(1,150,8))
# plot bar graph
plt.bar(range(len(rnd_lst)), rnd_lst, color = '#FFA726')
# show
plt.show()

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 6/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

keyboard_arrow_down Horizontal Bar Chart


Horizontal Bar Chart is very similar to normal Bar Chart except that a horizontal bar chart is a great option for long category names. This is
because there is space left of the chart for horizontal alignment of the axis labels.

Lets see an example!

# create random list of 8 numbers


rnd_lst = list(np.random.randint(1,150,8))
# plot bar graph
plt.barh(range(len(rnd_lst)), rnd_lst, color = '#FFA726')
# show
plt.show()

keyboard_arrow_down Error Bar Chart


Error bars are useful for showing estimated error or uncertainty and giving a general idea of how accurate a measurement is. It is done by
drawing. To visualize this information, error bars work by drawing lines extending from the center of the plotted data points or the edge of the
bar chart. As shown in the graph below, the length of the error bars helps reveal the uncertainty of the data points. Short error bars indicate that
the values are concentrated and the plotted mean is more likely, while longer error bars indicate that the values are more scattered and less
reliable. The length of each pair of error bars tends to be the same on both sides, but when the data are skewed, the lengths on both sides
become unbalanced.

Lets see the syntax:

matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt='', ecolor=None, elinewidth=None, capsize=None, barsabove=False, lolims=False, uplims=F

Syntax of plt.errorbar() is very similar to plt.bar() . The arguments that are very important are:

yerr : error term in y axis


xerr : error term in x axis

Check documentation

Lets see some example!

# defining our function


rndx = np.arange(10)/10
rndy = (rndx + 0.1)**2

# defining our error


y_error = np.linspace(0.05, 0.2, 10)

# plot error bars


plt.errorbar(rndx, rndy, yerr = y_error, fmt = '.-');

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 7/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

keyboard_arrow_down Stacked Bar Chart


A stacked chart is a type of bar chart that shows how two or more variables compare over time. Also called a stacked bar or column chart, it
looks like a series of columns or bars that are stacked on top of each other. Stacked charts are very useful for comparisons when used
correctly. They are designed to compare total values across categories.

Advantages
can represent multiple categories in tight space
good for showing change over time of category sub-components

Disadvantages
can be challenging to understand
cab become very crowded

Lets see a very simple example!

# lets create two random lists :: will be stacked on top of each other
rnd_a = list(np.random.randint(1,200,10))
rnd_b = list(np.random.randint(1,200,10))
# use simple range function for marking x steps
x_steps = range(10)

# for a stacked bar we will use two separte plt.bar() before calling plt.show()
plt.bar(x_steps, rnd_a, color = 'b')
plt.bar(x_steps, rnd_b, color = 'r', bottom = rnd_a)

plt.show()

keyboard_arrow_down Grouped Bar Chart


Grouped Bar Chart is trying to solve the main drawback of Stacked Bar chart by having a spearte bar for every sub-group (sub-category) while
havig the related sub-groups aligned horizontaly.

Lets see how!

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 8/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab
# create two random lists that are reresents the amount bought of product a, and b respectively
prod_a = list(np.random.randint(1,200,5))
prod_b = list(np.random.randint(1,200,5))

# the label locations


x = np.arange(len(prod_a))
width = 0.35 # the width of the bars

# create two
plt.bar(x - width/2, prod_a, width, label='Men')
plt.bar(x + width/2, prod_b, width, label='Women')
# show
plt.show()

keyboard_arrow_down Pie Plot


Pie plots can only be used when the individual parts add up to a meaningful whole and are used to visualize how each part contributes to the
whole.

Advantages
know the part-to-whole relationship

Disadvantages
Sometimes when plotting a pie plot with some orientation in the 3rd dimension it will be very misleading.

Lets see the syntax:

matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=0, rad

The most important arguments

x : main array to visulaize to make. they should contribute to a whole.


labels : label names corresponding to every element in x
explode : for creating exploded pie chart
startangle : the angle from which it will start drawing

Check documentation

Lets see an example!

# random array
rndnums = np.array([35, 28, 25, 10])
# labels
labels = ['A', 'B', 'C', 'D']
# create pie plot
plt.pie(rndnums, labels = labels)
# show
plt.show()

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 9/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

Lets try to use explode and startangle

# random array
rndnums = np.array([35, 28, 25, 10])
# labels
labels = ['A', 'B', 'C', 'D']
# create pie plot
plt.pie(rndnums, explode=(0.1, 0, 0, 0), startangle=18, labels = labels)
# show
plt.show()

keyboard_arrow_down Box Plot


Box Plot, also known as Whisker plot, is created to display the summary of the set of data values using properties like minimum, first quartile,
median, third quartile and maximum. In the box plot, a box is created from the first quartile to the third quartile, a vertical line is also there which
goes through the box indicating the median. Here x-axis denotes the data to be plotted while the y-axis shows the frequency distribution.

When dealing with numerical data, Box Plot is the best as it's revealing a lot of information that will help you get some insights about
distrbution, range, outliers, and skewness.

Lets see the syntax.

matplotlib.pyplot.boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None, widths=None, patch_artist=None, bootstrap=None, usermedia

Check documentation

Lets see a simple example!

# create random array of numbers


rnd_nums = np.random.randn(500)
# plot boxplot
plt.boxplot(rnd_nums, notch=True)
# show
plt.show();

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 10/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

keyboard_arrow_down Area Plot


Area Plot is very similar to Line Plot. Its major use is when we want to visualize and stress on the difference between two separate Line Plot

Lets see the syntax of it.

matplotlib.pyplot.fill_between(x, y1, y2=0, where=None, interpolate=False, step=None, *, data=None, **kwargs)

x : x values
y1 : first border of y-axis
y2 : second border of y-axis, if there's one! Check documentation

Lets see an example!

# random x values
rndx = np.linspace(0, 10, 2000)
# y1 = sin(randx)
y1 = np.sin(rndx)
# y2 = cos(randx)
y2 = np.cos(rndx)
# create first plot
plt.plot(rndx, y1 , label = 'Sin(X)')
# create second plot
plt.plot(rndx, y2 , label = 'Cos(X)')
# use fill between the x-axis (y=0), and y1
plt.fill_between(rndx, y1=0, y2=y1)
# use fill between the x-axis (y=0), and y2
plt.fill_between(rndx, y1=0, y2=y2)
# show
plt.show()

keyboard_arrow_down Histogram
Histograms or frequency distribution plots indicates how often each distinct value occurs in a data set. Histograms are the most commonly
used charts for showing frequency distributions. Although they are very similar to bar charts, there are important differences between the two.
Histograms visualize quantitative data or numerical data, whereas bar charts display categorical variables.

Histograms are the best to use when you have a range of numerical data and you want to make sure about their distrbution. Knowing the
distrbution of a numerical feature can indicate a lot about the quality of the process resulting this feature and also, can hugly direct your choice
to your machine learning model, if you will proceed to this stage in the data science pipeline

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 11/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab
Lets see Histogram syntax:

matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientat

The most important arguments are:

x : variable that should be drafted


bins : number of bins that should be included in a plot

Check documentation

Lets see some code example!

# create random normal distrbution


rndx = np.random.normal(size = 2000)
# create histogram
plt.hist(rndx, bins=40, color='yellowgreen')
# show
plt.show()

keyboard_arrow_down Scatter Plot


Scatter Plot is a type of graph that shows the relationship between two numerical values by plotting their individual points on a graph. Each
point is represented by a dot.

Advantages
Creating scatter diagrams is a simple task, as all that needs to be done is to place and mark points on a graph.
Scatter plots are simple to comprehend and decipher.
Points that are far got isolated from the majority of points can be disregarded when conducting a correlation analysis, as these outliers do
not significantly affect the results.

Disadvantages
Scatter plots can become cluttered and difficult to read when they contain a large number of data points.
It can be difficult to determine the exact correlation between variables using a scatter plot, especially if the data points are widely
dispersed.
Scatter plots are not effective for displaying certain types of data, such as categorical data.
It can be difficult to accurately determine the distribution of the data using a scatter plot.

Lets see its syntax:

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolor

The most important values are:

x : array or Series that hold the values of x


y : array or Series that hold the values of y

Check documentation

Lets see some example!

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 12/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

# generate random data


x = np.random.normal(size=100)
y = np.random.normal(size=100)

# plot the data


plt.scatter(x, y)
# show the plot
plt.show()

keyboard_arrow_down Hexbin Plot


Hexbin Plot is a useful way to represent the relationship between two numerical variables when you have a large amount of data. In this type of
plot, the data is divided into hexagonal bins, and the color of each bin indicates the number of data points it contains. This can help to reduce
overlap between points and provide a clearer visual representation of the data distribution. It's simply a way to overcome the main drawback of
scatter plot

Lets see its syntax!

matplotlib.pyplot.hexbin(x, y, C=None, gridsize=100, bins=None, xscale='linear', yscale='linear', extent=None, cmap=None, norm=None, vmin=None, vm

The most important arguments:

x : array or Series that hold the values of x


y : array or Series that hold the values of y

Lets see an example!

# create data
x = np.random.normal(size=20000)
y = (x * 0.1 + np.random.normal(size=20000)) * 5

# make the plot


plt.hexbin(x, y, gridsize=(20,20) )
plt.show()

The brighter areas are indicating the most densed areas in the points distrbutions

keyboard_arrow_down Subplots
https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 13/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab
Plots in matplotlib must be implied within a Figure object. we can create a new figure with plt.figure() as follows:

fig = plt.figure()

We create one or more subplot using fig.add_subplot() as follows:

ax1 = fig.add_subplot(2, 2, 1)

The above command means that there are four plots (2 * 2 = 4). We selected the first of four subplots. We can create the next three subplots
using the fig.add_subplot() commands as follows:

ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax4 = fig.add_subplot(2, 2, 4)

There's also another way in creating subplots and it may be easier. It's syntax is as follows:

fig , ax = plt.subplots(nrows , ncols, figsize)

This will create a figure and axis that could be accessed just like an array.

In a more visualized form we can imagine these subplots as follows:

Lets create our first subplot:

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 14/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab
# display multiple plots in one figure using subplots()
# create range of numbers from -50 to 50
x = np.arange(-50,50)
# square function
y_sq = np.power(x,2)
# cubic function
y_cub = np.power(x,3)
# sin function
y_sin = np.sin(x)
# cos function
y_cos = np.cos(x)
# tan function
y_tan = np.tan(x)
# tanh function
y_tanh = np.tanh(x)
# sinh function
y_sinh = np.sinh(x)
# cosh function
y_cosh = np.cosh(x)
# exp function
y_exp = np.exp(x)
# lets create figure and subplot
fig , ax = plt.subplots(nrows=3, ncols=3 , figsize = (20,20))
# plot y_sq
ax[0,0].plot(x,y_sq,"tab:blue")
# plot y_cub
ax[0,1].plot(x,y_cub,"tab:orange")
# plot y_sin
ax[0,2].plot(x,y_sin,"tab:green")
# plot y_cos
ax[1,0].plot(x,y_cos,"b-")
# plot y_tan
ax[1,1].plot(x,y_tan,"r-")
# plot y_tanh
ax[1,2].plot(x,y_tanh,"g-")
# plot y_sinh
ax[2,0].plot(x,y_sinh,"m-")
# plot y_cosh
ax[2,1].plot(x,y_cosh,"y-")
# plot y_exp
ax[2,2].plot(x,y_exp,"k-")
# show
plt.show()

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 15/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

keyboard_arrow_down Styling your plots in matplotlib


Styles
Matplotlib always add some plotting styles to its framework. These styles can enhance the plot style. To view the avaliable styles

print(plt.style.available)

To select a specific style. Use the following:

plt.style.use('style_name')

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 16/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

# print available styles


print(plt.style.available)

['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh', 'classic', 'dark_background', 'fast', 'fivethir

# setting a style for the graph


plt.style.use('seaborn')
# now, lets plot a simple plot
# create random array
rnd_arr = np.arange(0, 8, 0.01)
# plot function
plt.plot(rnd_arr, [i**3 for i in rnd_arr ])
# show the plot
plt.show()

Now, the style of the graph has changed!

keyboard_arrow_down Resize figures


One of the most things you will need to do with plots is to change the size of a plot. Lets see the syntax.

matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, *, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.

- figsize: Width, height in inches


- dpi: Dots-per-Inch
- facecolor: background color
- edgecolor: edge color

Check documentation.

All the color options in matplotlib can be defined using the hex code of this color. You can get the color code of any color you want from Adobe
color wheel for example.

Lets see some example!

# create random array


rnd_arr = np.arange(0, 8, 0.01)
# specify figure size
plt.figure(figsize=(8, 8))
# plot function
plt.plot(rnd_arr, [i**3 for i in rnd_arr ])
# show the plot
plt.show()

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 17/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

keyboard_arrow_down Adding a grid


Adding a grid to a plot can be useful for a number of reasons:

It can help to make the plot easier to read and interpret by providing a frame of reference for the data.
It can help to highlight patterns or trends in the data that might not be immediately apparent otherwise.
It can make it easier to compare values between different data points.
It can improve the overall aesthetics of the plot.

Overall, adding a grid to a plot can be a useful tool for helping to communicate the key insights and findings of your data analysis.

Syntax of grid:

plt.grid(True)

Check documentation

Lets see some example!

# grid is on by default in the previous style so, we will change it!


plt.style.use('seaborn-whitegrid')
# create randx
rndx = np.arange(1, 5)
# plot
plt.plot(rndx, rndx*1.5)
# adding grid on
plt.grid(True)
# show
plt.show()

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 18/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

keyboard_arrow_down Handling Axes


In some cases, you will need to control the axes limits in your plot in order to focus in some part of the data.

Syntax of controlling axes:

matplotlib.pyplot.axis(*args, emit=True, **kwargs)

and it can be called in different ways.

xmin, xmax, ymin, ymax = axis()


xmin, xmax, ymin, ymax = axis([xmin, xmax, ymin, ymax])
xmin, xmax, ymin, ymax = axis(option)
xmin, xmax, ymin, ymax = axis(**kwargs)

# calling plot
plt.plot(rndx, rndx*1.5)
# setting xmin, xmax, ymin, ymax
plt.axis([0, 5, -1, 13])
# show
plt.show()

In some times, we will need to control the x-axis or y-axis separtely. We could use the following:

plt.xlim([xmin, xmax])
plt.ylim([ymin, ymax])

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 19/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab
# calling plot
plt.plot(rndx, rndx*1.5)
# setting xlim
plt.xlim([1.0, 4.0])
# setting ylim
plt.ylim([0.0, 12.0])
# show
plt.show()

keyboard_arrow_down Handling X and Y ticks


X and Y ticks are markers placed along the x and y axes of a plot that help to identify the values of the plotted variables. Handling x and y ticks
can make the plot easier to read and interpret, improve its aesthetics, and make it easier to compare values between plots.

Syntax!

plt.xticks(list_of_ticks)
plt.yticks(list_of_ticks)

Lets see some example!

# create random numbers


rnd_nums = [1, 3, 4, 5, 6, 7, 8, 10]
# plot
plt.plot(rnd_nums)
# specify x and y ticks
plt.xticks([2, 4, 6, 8, 10])
plt.yticks([2, 4, 6, 8, 10])
# show
plt.show()

keyboard_arrow_down Adding Labels and Title


https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 20/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab
Labels, that can be put over axes, are a very important piece when it come to interpreting your graphs. Title also, is a very important aspect for
interpretation.

Adding a label for x-axis.

matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)

This method will enable you adding xlabel, controlling its font, controlling its loc, etc.. Check documentation

Adding a label for y-axis. This is very similar to the above.

Adding a title for the plot

matplotlib.pyplot.title(label, fontdict=None, loc=None, pad=None, *, y=None, **kwargs)

It's also very similar to the plt.xlabel() . Check documentation

Lets see some example!

# setting a style for the graph


plt.style.use('seaborn')
# create random list of 8 numbers
rnd_lst = list(np.random.randint(1,150,8))
# categories list
cat_lst = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
# plot bar graph
plt.bar(cat_lst, rnd_lst, color = '#FFA726')
# setting xlabel
plt.xlabel('Products', fontdict={'fontsize':10})
# setting ylabel
plt.ylabel('Consumption', fontdict={'fontsize':10})
# setting title
plt.title('Consumption of Products', fontdict={'fontsize':12})
# show
plt.show()

keyboard_arrow_down Adding a legend


Legend is a graphical element that explains the symbols or colors used in a chart or graph, helping the viewer to understand the data being
presented. It provides context and meaning to the data by showing what the different symbols or colors represent.

Syntax:

matplotlib.pyplot.legend(*args, **kwargs)

It can be called in many different ways:

legend()
legend(handles, labels)

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 21/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab
legend(handles=handles)
legend(labels)

Lets see an example!

# generate x range
x = np.arange(0,5)
# cubic power function
y_cub = np.power(x,3)
# sqrt function
y_sqrt = np.sqrt(x)
# cos function
y_cos = np.cos(x)

# plot
plt.plot(x, y_cub)
plt.plot(x, y_sqrt)
plt.plot(x, y_cos)
# adding a legend
plt.legend(['Cubic','Sqrt','Cosine'])
# show
plt.show()

Also, it can be implemented in a different way!

# generate x range
x = np.arange(0,5)
# cubic power function
y_cub = np.power(x,3)
# sqrt function
y_sqrt = np.sqrt(x)
# cos function
y_cos = np.cos(x)

# plot
plt.plot(x, y_cub, label='Cubic')
plt.plot(x, y_sqrt, label='Sqrt')
plt.plot(x, y_cos, label='Cosine')
# adding a legend
plt.legend()
# show
plt.show()

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 22/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

plt.legend() also, have an argument called loc for controlling the location of the legend. loc can take different values as follows:

best
upper left
upper right
lower left
lower right
upper center
lower center
center left

# generate x range
x = np.arange(0,5)
# cubic power function
y_cub = np.power(x,3)
# sqrt function
y_sqrt = np.sqrt(x)
# cos function
y_cos = np.cos(x)

# plot
plt.plot(x, y_cub, label='Cubic')
plt.plot(x, y_sqrt, label='Sqrt')
plt.plot(x, y_cos, label='Cosine')
# adding a legend
plt.legend(loc='lower left')
# show
plt.show()

keyboard_arrow_down Color
There are differnet ways to control the plot color in matplotlib:

You can use a single color string such as red , blue , or green .

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 23/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab
You can use a short color string such as r , b , or g .
You can use a hex string such as #FF0000 for red or #0000FF for blue.
You can use a tuple of values in the range 0-1, such as (0.1, 0.2, 0.5) for a custom color.

Lets see some examples!

Using color string

# create rnd_x :: 1000 random point between 0 and 1


rnd_x = np.linspace(0, 10, 1000)
# create rnd_y :: cos value of each point in x
rnd_y = np.cos(rnd_x)
# plot
plt.plot(rnd_x, rnd_y, color = 'red')
# show
plt.show();

Using color string short

# plot
plt.plot(rnd_x, rnd_y, color = 'g')
# show
plt.show();

using hex code

# plot
plt.plot(rnd_x, rnd_y, color = '#0000FF')
# show
plt.show();

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 24/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

Using RGB color

# plot
plt.plot(rnd_x, rnd_y, color = (0.1, 0.2, 0.5))
# show
plt.show();

keyboard_arrow_down Controlling Line Styles


Also, the you can control the line style in each graph using the linestyle parameter.

solid - a solid line


dotted - a dotted line
dashed - a dashed line
dashdot - a dash-dot line

Lets see some example!

# plot
plt.plot(rnd_x, rnd_y, linestyle = 'dashdot')
# show
plt.show();

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 25/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab

keyboard_arrow_down Styling Subplots


Now, lets see an example for using all the styling techniques and see how this works.

# display multiple plots in one figure using subplots()


# create range of numbers from -50 to 50
x = np.arange(-50,50)
# square function
y_sq = np.power(x,2)
# cubic function
y_cub = np.power(x,3)
# sin function
y_sin = np.sin(x)
# cos function
y_cos = np.cos(x)
# tan function
y_tan = np.tan(x)
# tanh function
y_tanh = np.tanh(x)
# sinh function
y_sinh = np.sinh(x)
# cosh function
y_cosh = np.cosh(x)
# exp function
y_exp = np.exp(x)
# lets create figure and subplot
fig , ax = plt.subplots(nrows=3, ncols=3 , figsize = (15,15))
# plot y_sq
ax[0,0].plot(x,y_sq,"tab:blue")
ax[0,0].set_title('Square Function')
ax[0,0].set_xlabel('X', fontsize = 12)
ax[0,0].set_ylabel('Y', fontsize = 12)
# plot y_cub
ax[0,1].plot(x,y_cub,"tab:orange")
ax[0,1].set_title('Cubic Function')
ax[0,1].set_xlabel('X', fontsize = 12)
ax[0,1].set_ylabel('Y', fontsize = 12)
# plot y_sin
ax[0,2].plot(x,y_sin,"tab:green")
ax[0,2].set_title('Sin Function')
ax[0,2].set_xlabel('X', fontsize = 12)
ax[0,2].set_ylabel('Y', fontsize = 12)
# plot y_cos
ax[1,0].plot(x,y_cos,"b-")
ax[1,0].set_title('Cosine Function')
ax[1,0].set_xlabel('X', fontsize = 12)
ax[1,0].set_ylabel('Y', fontsize = 12)
# plot y_tan
ax[1,1].plot(x,y_tan,"r-")
ax[1,1].set_title('Tangent Function')
ax[1,1].set_xlabel('X', fontsize = 12)
ax[1,1].set_ylabel('Y', fontsize = 12)
# plot y_tanh
ax[1,2].plot(x,y_tanh,"g-")
ax[1,2].set_title('Hyperpolic Tangent')
ax[1,2].set_xlabel('X', fontsize = 12)
https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 26/27
7/19/24, 9:57 AM 5-plotting-with-matplotlib.ipynb - Colab
ax[1,2].set_ylabel('Y', fontsize = 12)
# plot y_sinh
ax[2,0].plot(x,y_sinh,"m-")
ax[2,0].set_title('Hyperpolic Sin')
ax[2,0].set_xlabel('X', fontsize = 12)
ax[2,0].set_ylabel('Y', fontsize = 12)
# plot y_cosh
ax[2,1].plot(x,y_cosh,"y-")
ax[2,1].set_title('Hyperpolic Cosine')
ax[2,1].set_xlabel('X', fontsize = 12)
ax[2,1].set_ylabel('Y', fontsize = 12)
# plot y_exp
ax[2,2].plot(x,y_exp,"k-")
ax[2,2].set_title('Exponential Function')
ax[2,2].set_xlabel('X', fontsize = 12)
ax[2,2].set_ylabel('Y', fontsize = 12)
# show
plt.show()

https://colab.research.google.com/drive/1sYUfVTNB5gLADf46firzr8ckLucqzeZm#printMode=true 27/27

You might also like