Matplotlib
Matplotlib
Features of Matplotlib:
Highly customizable: You can control every aspect of a plot, including colors, labels,
and layout.
Multiple plot types: Supports line plots, bar plots, histograms, scatter plots, pie
charts, etc.
Integration with NumPy and Pandas: Works seamlessly with these libraries for
data manipulation and visualization.
Interactive plots: Can create interactive figures using matplotlib.pyplot or
integrate with GUI backends.
Exporting capabilities: Plots can be saved as various file formats, including PNG,
PDF, and SVG.
The Figure is the final image, and may contain one or more Axes.
Matplotlib Architecture
Back-end layer:
Back-end layer of matplotlib provides the implementations of three abstract interface classes.
FigureCanvas:
Example: for drawing in real world, we need paper onto which we can draw. FigureCanvas is
similar to paper.
matplotlib.backend_bases.FigureCanvas
Renderer:
FigureCanvas.
Example: just like we need paintbrush, pen or pencil to draw on paper, we use Renderer object
to draw on FigureCanvas.
matplotlib.backend_bases.Renderer
Event:
matplotlib.backend_bases.Event
Artist layer:
draw on canvas.
Everything we see on matplotlib figure is an instance of Artist. Example: title, lines, labels,
images etc.
Primitive:
Composite:
Each composite artist may contain other composite artist as well as primitive artist.
Artist layer is best for the programmers and appropriate paradigm when writing a web
Scripting layer:
Artist layer is syntactically heavy for the everyday purposes, especially for exploratory work.
Scripting layer composed mainly of pyplot, a lighter interface than Artist layer.
matplotlib.pyplot
Example:
With Pyplot, you can use the grid() function to add grid lines to the plot.
You can use the axis parameter in the grid() function to specify which grid
lines to display.
Legal values are: 'x', 'y', and 'both'. Default value is 'both'.
plt.grid(axis = 'x')
You can also set the line properties of the grid, like this: grid(true, color =
'color', linestyle = 'linestyle', linewidth = number).
Plt.grid(true) # add both grid lines to background
Markers
You can use the keyword argument marker to emphasize each point with a
specified marker:
You can also use the shortcut string notation parameter to specify the
marker.
This parameter is also called fmt, and is written with this syntax:
marker|line|color
plt.plot(ypoints, 'o:r')
Line Reference
Line syntax Description
‘-‘ Solid line
‘:’ Dotted line
‘- -‘ Dashed line
‘- .’ Dashed/dotted line
Colour
We can also set the line style of a line chart using the linestyle parameter. It can take a string
such as "solid", "dotted", "dashed" or "dashdot".
Legend
In matplotlib, a legend is a section describing the graph’s components. It helps us understand
the various curves and plots in our figures.
plt.legend(handles=[lone one,line two], loc=’lower right’,
shadow=True)
To add a legend outside our axes, we use the bbox_to_anchor() parameter
in legend() function
fontsize: To change the font size.
title: To add a title to the legend.
savefig()
The savefig() function is used to save the plot as a file. You can specify
the file format (e.g., .png, .jpg, .pdf) and other options like the DPI (dots
per inch).
The subplot() function takes three arguments that describes the layout of the figure.
plt.subplot(1, 2, 1)
#the figure has 1 row, 2 columns, and this plot is
the first plot.
plt.subplot(1, 2, 2)
#the figure has 1 row, 2 columns, and this plot is
the second plot.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 1)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 2)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 3)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 4)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 5)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 6)
plt.plot(x,y)
plt.show()
You can add a title to each plot with the title() function:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")
You can add a title to the entire figure with the suptitle() function:
plt.suptitle("MY SHOP")
Bar Chart
matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
Make a bar plot. The bars are positioned at x with the given alignment. Their dimensions are
given by height and width.
plt.show(
import numpy as np
import matplotlib.pyplot as plt
# data to plot
n_groups = 4
means_frank = (90, 55, 40, 65)
means_guido = (85, 62, 54, 20)
# create plot
index = np.arange(n_groups)
bar_width = 0.35
plt.bar(index, means_frank, bar_width,color='b',label='Frank')
plt.xlabel('Person')
plt.ylabel('Scores')
plt.title('Scores by person')
plt.xticks(index + bar_width/2, ('A', 'B', 'C', 'D'))
plt.legend()
plt.show()
Scatter Plot
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, alpha
=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwarg)
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y, color = 'hotpink')
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y, color = '#88c999')
plt.show()
Histogram
A histogram is a graph showing frequency distributions.
It is a graph showing the number of observations within each given interval.
This method uses numpy.histogram to bin the data in x and count the number of
values in each bin, then draws the distribution either as
a BarContainer or Polygon.
then the first bin is [1, 2) (including 1, but excluding 2) and the
second [2, 3). The last bin, however, is [3, 4], which includes 4.
range: tuple or None. The lower and upper range of the bins. Lower and upper
outliers are ignored. If not provided, range is (x.min(), x.max())
density: If True, draw and return a probability density: each bin will display the
bin's raw count divided by the total number of counts and the bin
width (density = counts / (sum(counts) * np.diff(bins))), so that the area
under the histogram integrates to 1 (np.sum(density * np.diff(bins)) == 1).
# create data
data = [32, 96, 45, 67, 76, 28, 79, 62, 43, 81, 70,
61, 95, 44, 60, 69, 71, 23, 69, 54, 76, 67,
82, 97, 26, 34, 18, 16, 59, 88, 29, 30, 66,
23, 65, 72, 20, 78, 49, 73, 62, 87, 37, 68,
81, 80, 77, 92, 81, 52, 43, 68, 71, 86]
# create histogram
plt.hist(data)
# display histogram
plt.show()
# Create a histogram
plt.hist(data, bins=30, color='blue', alpha=0.7, edgecolor='black')
vert: (default: True) If True, draws vertical boxes. If False, draw horizontal
boxes.
Patch_artist: (default: False) If False produces boxes with the Line2D artist.
Otherwise, boxes are drawn with Patch artists.
widths: The widths of the boxes. The default is 0.5,
or 0.15*(distance between extreme positions), if that is smaller.
Box Plot Figure
# Library Import (matplotlib)
import matplotlib.pyplot as plot
value_A = [62,77,44,41,77,69,79,79,75,35,48,81,75,64,72,88,81,56,54,59]
value_B = [ 64,52,81,45,35,31,91,98,19,91,90,52,29,53,103,16,75,13,33,23]
value_C = [25,29,19,98,52,81,21,61,65,85,12,54,12,56,36,55,35,32,22,82]
value_D = [89,75,71,19,88,66,89,99,70,80,89,78,14,29,75,86,79,91,73,90]
value_E = [90,57,76,40,18,88,65,81,58,19,47,89,32,36,43,52,18,58,19,95]
box_plot_data=[value_A,value_B,value_C,value_D,value_E]
plot.title("Fruit Growth Distribution")
plot.boxplot(box_plot_data,patch_artist=True,labels=['Banana','Pineapple','Peach','Grapes','M
uskmelon'])
plot.show()
Heatmaps
A heatmap is a graphical representation of data where individual values are represented as
colors. In the context of a scatter dataset, a heatmap can show the density of data points in
different regions of the plot. This can be particularly useful for identifying clusters, trends,
and outliers in the data.
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(1000)
y = np.random.randn(1000)
heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)