Data Visualization and Matplotlib
Data Visualization and Matplotlib
https://www.w3schools.com/pytho
n/matplotlib_scatter.asp
Data Visualization
● Data Visualization refers to the graphical or visual representation of information and data
using visual elements like charts, graphs, maps etc
● It is immensely useful in decision making to understand the meaning of data to drive
business decision
● For data visualization in Python the Matplotlib library’s pyplot is used
● Pyplot is a collection of methods within matplotlib library of python to construct 2D plot
easily and interactively
● Matplotlib library is preinstalled with Anaconda distribution
● In order to use pyplot for data visualization, user need to import the pyplot module as
follows:
import matplotlib.pyplot as plt
What is Matplotlib?
Matplotlib is a low level graph plotting library in python that serves as a visualization utility.
Matplotlib is mostly written in python, a few segments are written in C, Objective-C and Javascript for Platform
compatibility.
Installation of Matplotlib
If you have Python and PIP already installed on a system, then installation of Matplotlib is very easy.
Import Matplotlib
Once Matplotlib is installed, import it in your applications by adding the import module statement:
import matplotlib
import matplotlib
print(matplotlib.__version__)
output
2.0.0
Matplotlib Pyplot
Pyplot
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under the
plt alias:
Types of Plots
● Line
● Bar
● Scatter
● histogram
● Pie etc.
Matplotlib Plotting
Plotting x and y points
The plot() function is used to draw points (markers) in a diagram.
Example
Draw a line in a diagram from position (1, 3) to position (8, 10):
plt.plot(xpoints, ypoints)
plt.show()
Plotting Without Line
To plot only the markers, you can use shortcut string notation parameter 'o', which means
'rings'.
Example:
Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):
import numpy as np
plt.show()
Multiple Points
We can plot as many points as we like,but there should be same number of points in both axis.
Example
Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8, 10):
import numpy as np
plt.plot(xpoints, ypoints)
plt.show()
Default X-Points
If we do not specify the points on the x-axis, they will get the default values 0, 1, 2, 3 (etc., depending on
the length of the y-points. So, if we take the same example as above, and leave out the x-points The
x-points in the example will take [0, 1, 2, 3, 4, 5], the diagram will look like this:
import numpy as np
plt.plot(ypoints)
plt.show()
Matplotlib Markers
Markers
You can use the keyword argument marker to emphasize each point with a specified marker:
...
plt.plot(ypoints, marker = '*')
...
'*' Star
'^' Triangle Up
'.’ Point
'<' Triangle Left
',' Pixel
'>' Triangle Right
'X’ X
'1' Tri Down
'X' X (filled)
'2' Tri Up
'+' Plus
'3' Tri Left
'P' Plus (filled)
'4' Tri Right
'S' Square
'|' Vline
'D' Diamond
'_' Hline
'D' Diamond (thin)
Format Strings fmt
You can use 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
Color Reference
Color Syntax Description
'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
We can also use Hexadecimal color values by giving color prefixed with # followed by hexadecimal
code for red, green and blue from 0-9 and A-F, eg: #3CAA9F
We can also give color names like red, blue , green, pink, hotpink etc
Example Mark each point with a circle dotted line in red
color:
import numpy as np
plt.plot(ypoints, 'o:r')
plt.show()
Marker Size
The keyword argument markersize or the shorter version, ms is used to set the size of the markers:
import numpy as np
plt.show()
Marker Color
The keyword argument markeredgecolor or the shorter mec to set the color of the edge of the markers:
import numpy as np
plt.show()
Markerfacecolor keyword:
The keyword argument markerfacecolor or the shorter mfc is used to set the color inside the edge of
the markers:
import numpy as np
plt.show()
Using both the mec and mfc arguments to color the entire marker:
Example Set the color of both the edge and the face to red:
import numpy as np
plt.show()
Example
Mark each point with a beautiful green color using hexadecimal code for coloring markeredgecolor mec and
markerfacecolor mfc:
import numpy as np
plt.show()
Matplotlib Line
Linestyle
The keyword argument linestyle, or shorter ls, to change the style of the plotted line:
Line Styles
Style Or
'dotted' ':'
'dashed' '--'
'dashdot' '-.'
import numpy as np
plt.show()
Example:
Use a dashed line:
Shorter Syntax
The line style can be written in a shorter syntax:
Example
plt.plot(ypoints, ls = ':')
Line Color
The keyword argument color or the shorter c is used to set the color of the line:
Color Reference
Color Syntax Description
'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
We can also use Hexadecimal color values by giving color prefixed with # followed by hexadecimal code for red,
green and blue from 0-9 and A-F, eg: #3CAA9F
We can also give color names like red, blue , green, pink, hotpink etc
Example
Set the line color to red:
Example
Plot with a beautiful green line:
plt.plot(ypoints, c = '#4CAF50')
Example
Plot with the color named "hotpink":
plt.plot(ypoints, c = 'hotpink')
Line Width
You can use the keyword argument linewidth or the shorter lw to change the width of the line.
import numpy as np
plt.show()
Multiple Lines
We can plot as many lines as needed by simply adding more plt.plot() functions:
Example
Draw two lines by specifying a plt.plot() function for each line:
import numpy as np
y1 = np.array([3, 8, 1, 10])
y2 = np.array([6, 2, 7, 11])
plt.plot(y1)
plt.plot(y2)
plt.show()
Example
Draw two lines by specifiyng the x- and y-point values for both lines:
x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 10])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 7, 11])
Example
Add labels to the x- and y-axis:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.show()
Example : Add a plot title and labels for the x- and y-axis:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110,
115, 120, 125])
y = np.array([240, 250, 260, 270, 280,
290, 300, 310, 320, 330])
plt.plot(x, y)
plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.show()
Matplotlib Adding Grid Lines
Add Grid Lines to a Plot
The grid() function with Pyplot is used to add grid lines to the plot
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'.
Example
Add grid lines to the plot:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([ 80, 85, 90, 95, 100, 105,
110, 115, 120, 125])
y = np.array([ 240, 250, 260, 270, 280,
290, 300, 310, 320, 330])
plt.title( "Sports Watch Data" )
plt.xlabel( "Average Pulse" )
plt.ylabel( "Calorie Burnage" )
plt.plot(x, y)
plt.grid()
plt.show()
Example
Display only grid lines for the x-axis:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.grid(axis = 'x')
plt.show()
Example
Display only grid lines for the y-axis:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.grid(axis = 'y')
plt.show()
plt.plot(x, y)
plt.show()
Matplotlib Subplot
Display Multiple Plots
The subplot() function is used to draw multiple plots in one figure:
The layout is organized in rows and columns, which are represented by the first and second argument.
Example
Draw 2 plots:
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.show()
Example
Draw 2 plots on top of each other:
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 1, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 1, 2)
plt.plot(x,y)
plt.show()
Example plt.subplot(2, 3, 4)
Draw 6 plots: plt.plot(x,y)
plt.subplot(2, 3, 2) plt.show()
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])
Title
The title to each plot can be added with the title() function
Super Title
The title to the entire figure is added with the suptitle() function
Example
Add a title and super title for the entire figure:
#plot 1:
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")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")
#super title
plt.suptitle("MY SHOP")
plt.show()
Matplotlib Scatter
Creating Scatter Plots
With Pyplot, the scatter() function is used to draw a scatter plot.
The scatter() function plots one dot for each observation. It needs two arrays of the same length, one for the
values of the x-axis, and one for values on the y-axis
Example: A simple scatter plot:The observation in the example above is the result of 13 cars passing by.
The X-axis shows how old the car is. The Y-axis shows the speed of the car when it passes.
Compare Plots
In the example above, there seems to be a relationship between speed and age, but what if we plot the observations
from another day as well the plot can be show the relationship between two days car performance
plt.show()
Note: The two plots are plotted with two different colors, by default blue and orange we can change the colors
Colors
Color argument is used to set own color for each scatter plot or in short we can use c argument:
Example
Set your own color of the markers:
import matplotlib.pyplot as plt
import numpy as np
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()
Note: We cannot use the color argument for this, only the c argument is used
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])
colors = np.array(["red","green","blue","yellow","pink",
"Black","orange","purple","beige","brown","gray",
"cyan","magenta"])
plt.scatter(x, y, c=colors)
plt.show()
.
ColorMap
The Matplotlib module has a number of available colormaps.
A colormap is like a list of colors, where each color has a value that ranges from 0 to 100.
This colormap is called 'viridis' and it ranges from 0, which is a purple color, up to 100, which
is a yellow color.
In addition we have to create an array with values (from 0 to 100), one value for each point
in the scatter plot
colorbar()
Colormap can be included in the figure by using colorbar() function of pyplot lib
Example
Create a color array, and specify a colormap in the scatter plot:
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])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90,
100])
plt.show()
You can include the colormap in the drawing by including the plt.colorbar() statement:
import numpy as np
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])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60,
plt.colorbar()
plt.show()
import numpy as np
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])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70,
80, 90, 100])
plt.colorbar()
plt.show()
Available ColorMaps
Some of the the built-in colormaps:
Name Reverse
Accent Accent_r
Blues Blues_r
BrBG BrBG_r
BuGn BuGn_r
BuPu BuPu_r
CMRmap CMRmap_r
Dark2 Dark2_r
GnBu GnBu_r
Greens Greens_r
Greys Greys_r
OrRd OrRd_r
Oranges Oranges_r
PRGn PRGn_r
Paired Paired_r
Pastel1 Pastel1_r
Pastel2 Pastel2_r
PiYG PiYG_r
Size
The size of the dots can be changed using the s argument.
The array of sizes having same length as the array of for the x- and y-axis is passed as value for the
argument
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])
sizes = np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])
plt.scatter(x, y, s=sizes)
plt.show()
Alpha
The transparency of the dots can be set using the alpha argument. This take value ranging from 0 to 1, 0 being
more transparent and 1 more opaque
import numpy as np
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])
sizes = np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])
plt.show()
Create random arrays with 100 values for x-points, y-points, colors and sizes:
import numpy as np
x = np.random.randint(100, size=(100))
y = np.random.randint(100, size=(100))
plt.colorbar()
plt.show()
Visualizing Errors
data visualization is the graphical representation of information and data by using visual elements like charts graphs and maps.
data visualization tools provide an accessible way to see and understand trends outliers and patterns in data.
error bars help to indicate estimated error or uncertainty to give a general sense of how precise a measurement is done, through
the use of markers drawn over the original graph and its data points.
Typically error bars are used to display either the standard deviation standard error confidence intervals or the minimum and
maximum values in ranged dataset.
● Defining Libraries: Import the libraries which are required to plot error bars (For data
creation and manipulation: Numpy, For data visualization: pyplot from matplotlib).
● Define X and Y: Define the data values used for plotting. Data values of x-axis and
y-axis.
● Plot error bars: By using the errorbar() method we can plot the error bars.
● Display: Finally we have to use the show() method to display the plot.
matplotlib.pyplot.errorbar() Function:
The errorbar() function in pyplot module of matplotlib library is used to plot y versus x as lines
and/or markers with attached errorbars.
The syntax to plot error bars is as below:
matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt='', ecolor=None,
elinewidth=None, capsize=None, barsabove=False, lolims=False, uplimes=False,
xlolims=False, xuplims=False, errorevery=1, capthick=None, * , data=None,
**kwargs)
● fmt: Contains string value. By default, this plot error bars with markers. Use ‘none’ to
plot error bars without markers.
● barsabove: It contains bool value. By default value is False, if the value is True error
bars are plotted above the plot symbol.
● lolims,uplims,xlolims,xuplims: specifies that value gives only upper and lower limits.
It contains bool value.
● errorevery: It contains integer values and is used to draw error bars on the subset of
the data.
Example
# Import Library
import matplotlib.pyplot as plt
# Define Data
x= [1, 2, 3, 5]
y= [9, 15, 20, 25]
# Display graph
plt.show()
● In the above, example we import the matplotlib.pyplot library.
● Then we define the x-axis and y-axis data points.
● plt.errorbar() method is used to plot error bars and we pass the argument x,
y, and xerr and set the value of xerr = 0.9. Which is the length of the error
line
● Then we use plt.show() method to display the error bar plotted graph.
Let’s change the following things in the error bars to become it more interactive:
# Import Library
import matplotlib.pyplot as plt
# Define Data
x= [1, 2, 3, 5]
y= [9, 15, 20, 25]
# Display graph
plt.show()
The following are the cases in the bar chart in which we draw error bars:
● Error in x values
● Error in y values
● Error in both x and y values
Matplotlib chart error bars to plot error in x values
The plt.errorbar() method is used to plot the error bars and pass the argument xerr to plot error
on the x values.
matplotlib.pyplot.errorbar(x, y, xerr=None)
# Import Library
import matplotlib.pyplot as plt
# Define Data
x= [6, 15, 2.3, 9]
y= [9, 15, 20, 25]
# Define Error
x_error = [2.3, 5.1, 1, 3.1]
# Display graph
plt.show()
● In the above example, we import matplotlib.pyplot library and
define the data point on the x-axis and y-axis.
● Then we define the error value and use the plt.bar() method to
plot a bar chart.
● plt.errorbar() method is used to plot error bars and we pass
xerr as an argument and set its value to be x_error value which
are defined.
Example
# Import Library # Plot Bar chart
import matplotlib.pyplot as plt plt.bar(x,y)
● After this defines the data point on the x-axis and y-axis.
● Then we define the error value and use the plt.bar() method to plot a bar
chart and use plt.errorbar() method is used to plot error bars.
● Pass yerr as an argument and set its value to equal y_error value which we
define.
The syntax to plot error bars on both the values is as given below:
# Define Data
x= [6, 15, 2.3, 9]
y= [9, 15, 20, 25]
# Define Error
x_error = [4.2, 6, 10, 8.6]
y_error = [2.3, 5.1, 1, 3.1]
● In the above plot after drawing bar
# Plot Bar chart chart using the plt.bar() method , plot
plt.bar(x,y) error bars using plt.errorbar() method
● Pass xerr, yerr as an argument and
# Plot error bar
set its value to equal x_error, y_error
plt.errorbar(x, y, xerr = x_error, yerr = y_error,
fmt='o', ecolor = 'red',color='yellow') values which are define
# Display graph
plt.show()
The following are the cases in the scatter plot in which we draw error bars:
● Error in x values: Using errorbar() we plot the error bars and pass the argument xerr to
plot error on the x values in the scatter plot using syntax
matplotlib.pyplot.errorbar(x, y, xerr=None)
● Error in y values : Using errorbar() we plot the error bars and pass the argument yerr
to plot error on the y values in the scatter plot using syntax
matplotlib.pyplot.errorbar(x, y, yerr=None)
● Error in both x and y values: Using errorbar() we plot the error bars and pass the
arguments xerr, yerr to plot error on the x and y values in the scatter plot using syntax
matplotlib.pyplot.errorbar(x, y, yerr=None)
Example: Plotting error on x values in scatter plot
# Import Library
import matplotlib.pyplot as plt
# Define Data
x= [10, 20, 30, 40]
y= [4, 8, 12, 16]
# Define Error
x_error = [2, 4, 6, 8]
# Display graph
plt.show()
# Define Data
x= [10, 20, 30, 40]
y= [4, 8, 12, 16]
# Define Error
y_error = [2, 4, 6, 8]
# Display graph
plt.show()
Example: Plotting error on y values in scatter plot
# Import Library
import matplotlib.pyplot as plt
# Define Data
x= [10, 20, 30, 40]
y= [4, 8, 12, 16]
# Define Error
x_error= [3, 6, 9, 12]
y_error = [2, 4, 6, 8]
# Display graph
plt.show()
Density Plot
Density Plot is a type of data visualization tool. It is a variation of the histogram that uses
‘kernel smoothing’ while plotting the values. It is a continuous and smooth version of a
histogram inferred from a data.
Density plots uses Kernel Density Estimation (so they are also known as Kernel density
estimation plots or KDE) which is a probability density function. The region of plot with a
higher peak is the region with maximum data points residing between those values.
Density plots can be made using pandas, seaborn, etc. , we will generate density plots
using Pandas. We will be using two datasets of the Seaborn Library namely –
‘car_crashes’ and ‘tips’.
Syntax: pandas.DataFrame.plot.density | pandas.DataFrame.plot.kde
#define data
data = [value1, value2, value3, ...]
bw_method argument:
This argument is used to adjust the smoothness of density plot , if its
value is low it leads to ore wiggly plot and if the value is high leads to
smoother plot
Example 2: Adjust Smoothness of Density Plot
You can use the bw_method argument to adjust the smoothness of the density plot.
Lower values lead to a more “wiggly” plot.
#define data
data = [2, 2, 3, 5, 6, 6, 7, 8, 9, 10, 12, 12, 13, 15, 16]
You can also customize the color and style of the density plot:
#define data
data = [2, 2, 3, 5, 6, 6, 7, 8, 9, 10, 12, 12, 13, 15, 16]
A contour plot is appropriate if you want to see how alue Z changes as a function of two inputs X and Y, such
that Z = f(X,Y). A contour line or isoline of a function of two variables is a curve along which the function has a
constant value.
The independent variables x and y are usually restricted to a regular grid called meshgrid. The
numpy.meshgrid creates a rectangular grid out of an array of x values and an array of y values.
Matplotlib API contains contour() and contourf() functions that draw contour lines and filled contours,
respectively. Both functions need three parameters x,y and z.
Example:
import numpy as np
import matplotlib.pyplot as plt
xlist = np.linspace(0, 5, 50)
ylist = np.linspace(0, 5, 50)
x,y= np.meshgrid(xlist, ylist)
z = np.sin(x)**8+np.cos(20+y*x)*np.cos(y)
plt.contour(x,y,z)
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.title("Contour Plot Example")
plt.colorbar()
plt.show()
Note: The above example draw outlined contour plot
The contour plot can be formatted with attributes
colors=”name of the color” like red, blue, green, pink etc
linestyles=”style of line” like dashed, dasheddotted, dotted , solid(default style)
linewidths=numberic value (default is 1.5)
Eg:
plt.contour(x,y,z, linestyles=”dotted”,linewidths=”3”,colors=”red”)
plt.show()
Eg:
plt.contourf(x,y,z,cmap="Spectral", alpha=0.8)
plt.show()
Matplotlib Histograms
Histogram
A histogram is a graph showing frequency distributions. It is a graph showing the number
of observations within each given interval.
Each interval is represented with a bar, placed next to the other intervals on a number line.
The height of the bar represents the frequency of values in that interval.
Create Histogram
In Matplotlib, we use the hist() function to create histograms.
histtype optional parameter used to create type of histogram [bar, barstacked, step, stepfilled], default is “bar”
align optional parameter controls the plotting of histogram [left, right, mid]
rwidth optional parameter which is relative width of the bars with respect to bin width
Attribute parameter
label optional parameter string or sequence of string to match with multiple datasets
Example:
import numpy as np
import matplotlib.pyplot as plt
blood_sugar=[113,85,90,150,149,88,93,115,135,80,
77,82,129]
plt.hist(blood_sugar,bins=[80,100,125,150],
rwidth=0.9,color='r')
plt.show()
Example of drawing histogram using multiple datasets
import numpy as np
import matplotlib.pyplot as plt
men_blood_sugar=[113,85,90,150,149,88,93,
115,135,80,77,82,129]
women_blood_sugar=[67,98,89,120,133,
150,84,69,89,79,120,112,100]
plt.hist([women_blood_sugar,men_blood_sugar],
bins=[80,100,125,150],rwidth=0.9,
color=["#23A2AB","#ACFAA1"],label=['women','men'])
plt.legend()
plt.xlabel("Sugar Ranges")
plt.ylabel("Sugar Values")
plt.title("Sugar Readings vs Ranges")
plt.show()
Density Plot
A density plot is an extension of the histogram. As opposed to the histogram, the density
plot can smooth out the distribution of values and reduce the noise. It visualizes the
distribution of data over a given period, and the peaks show where values are concentrated.
Density Plot is the continuous and smoothed version of the Histogram estimated from the data. It
is estimated through Kernel Density Estimation.
In this method Kernel (continuous curve) is drawn at every individual data point and then all these
curves are added together to make a single smoothened density estimation.
Histogram fails when we want to compare the data distribution of a single variable over the
multiple categories at that time Density Plot is useful for visualizing the data.
Approach:
Example 3: Plotting the Density using seaborn library on the default setting.
# importing libraries
import seaborn as sns
import matplotlib.pyplot as plt
# importing libraries
import seaborn as sns
import matplotlib.pyplot as plt
Example 5: Plotting Histogram and Density Plot together by setting bins and color.
# importing libraries
import seaborn as sns
import matplotlib.pyplot as plt
There are various built-in styles in style package, and we can also write customized style files and,
then, to use those styles all you need to import them and apply on the graphs and plots. In this way,
we need not write various lines of code for each plot individually again and again i.e. the code is
reusable whenever required.
To list all the available styles:
print(style.available)
Output:
Syntax: plt.style.use(‘style_name”)
# creating a plot
plt.plot(data)
# show plot
plt.show()
Example
# importing all the necessary packages
import numpy as np
import matplotlib.pyplot as plt
# creating a plot
plt.plot(data)
# show plot
plt.show()
Note : But keep in mind that this will change the style for the rest of the session! Alternatively, you can use the
style context manager, which sets a style temporarily:
with plt.style.context('stylename'):
make_a_plot()
Customizing matplotlib
1. matplotlibrc in the current working directory, usually used for specific customizations that you do not want to
apply elsewhere.
2. It next looks in a user-specific place, depending on your platform:
○ On Linux, it looks in .config/matplotlib/matplotlibrc (or $XDG_CONFIG_HOME/matplotlib/matplotlibrc) if
you’ve customized your environment.
○ On other platforms, it looks in .matplotlib/matplotlibrc.
3. See .matplotlib directory location.
Dynamic rc settings
You can also dynamically change the default rc settings in a python script or interactively from
the python shell. All of the rc settings are stored in a dictionary-like variable called
matplotlib.rcParams , which is global to the matplotlib package. rcParams can be modified
directly, for example:
import matplotlib as mpl
mpl.rcParams['lines.linewidth'] = 2
mpl.rcParams['lines.color'] = 'r'
Matplotlib also provides a couple of convenience functions for modifying rc settings. The
matplotlib.rc() command can be used to modify multiple settings in a single group at once,
using keyword arguments:
import matplotlib as mpl
mpl.rc('lines', linewidth=2, color='r')
The matplotlib.rcdefaults() command will restore the standard matplotlib default settings.
Example
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from cycler import cycler
mpl.rcParams['lines.linewidth'] = 2
mpl.rcParams['lines.linestyle'] = '--'
data = np.random.randn(50)
plt.plot(data)
To create your own style sheet create a file with extension .mpltstyle
plt.use.style(‘my_awesome_style_sheet.mpltstyle')
A three-dimensional axes can be created by passing the keyword projection='3d' to any of the normal axes creation
routines.
import numpy as np
ax=plt.axes(projection="3d")
ax.scatter(3,5,7)
plt.show()
Example:
import matplotlib.pyplot as pyplot
import numpy as np
from mpl_toolkits import mplot3d
Example:
import matplotlib.pyplot as pyplot
import numpy as np
from mpl_toolkits import mplot3d
x = np.outer(np.linspace(-2, 4, 8), np.ones(8))
y = x.copy().T
z = np.cos(x ** 3 + y ** 4)
fig = pyplot.figure(figsize = (6, 6))
ax = pyplot.axes(projection = '3d')
ax.plot_surface(x, y, z, cmap ='viridis', edgecolor ='orange')
ax.set_title('PLOT with Yellow as least dense altitude and Black as denser altitude')
# Print the chart
pyplot.show()
Plotting 3D Wireframe plot
These are 3D plots that take a grid of values and project it onto the three-dimensional surface. The resulting generated
graphs become three-dimensional, which is easy to visualize. In the wireframe plot, the plotted surface does not remain
filled. They are simply wavey lines that form the grid-like structure showing the ups and downs of data. Professionals also
use such graphs to see changes in climate or ecosystem. The ax.plot_wireframe() function generates wireframe graph.
view_init()
view_init() can be used to rotate the axes programmatically.
Syntax: view_init(elev, azim)
Parameters: