0% found this document useful (0 votes)
13 views7 pages

Data Visualization with Matplotlib

The document provides a comprehensive guide on using the Matplotlib library for data visualization in Python. It covers installation, various types of plots (line, bar, scatter, pie, and 3D plots), and customization options such as annotations, legends, and logarithmic axes. The content includes code examples for creating different visualizations and emphasizes the importance of data representation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Data Visualization with Matplotlib

The document provides a comprehensive guide on using the Matplotlib library for data visualization in Python. It covers installation, various types of plots (line, bar, scatter, pie, and 3D plots), and customization options such as annotations, legends, and logarithmic axes. The content includes code examples for creating different visualizations and emphasizes the importance of data representation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Data Visualization Using Matplotlib Library

2. Learning Objectives

Introduction to Matplotlib
Matplotlib Installation
Data Visualization
Plotting and Visualization
Figures and Subplot
Colours, Marker and Line Style
Text Annotation
Three-Dimensional Plotting
Logarithmic Axes in Matplotlib
3. Introduction to Matplotlib
Matplotlib is a free and open-source drawing library that can work with a variety of drawing
formats.
You can generate plots, histograms, bar charts, and other types of charts with just a few lines
of code.
Matplotlib is an excellent Python visualisation library for two-dimensional array plots.
4. Installation
You'll need to install matplotlib first with either:
conda install matplotlib
or
pip install matplotlib
5. Data Visualization
The graphical representation of information and data is known as data visualisation
Data visualisation tools, which use visual elements such as charts, graphs, and maps, make it
easy to see and understand trends, outliers, and patterns in data

6. Importing the required module


pip install matplotlib
import [Link] as plt
# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]
# plotting the points
[Link](x, y)
# naming the x axis
[Link]('x - axis')
# naming the y axis
[Link]('y - axis')
# giving a title to my graph
[Link]('My first graph!')
# function to show the plot
[Link]()
import numpy as np
x = [Link](0, 10, 0.1)
y = [Link](x)

# figures in matplotlib using figure()


[Link](figsize=(10, 8))
[Link]("[Link]")
[Link](x, y)
[Link]()
7. subplots()
#plot 1:
x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])
[Link](2, 1, 1)
[Link](x,y)
#plot 2:
x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])
[Link](2, 1, 2)
[Link](x,y)
import [Link] as plt
import numpy as np
ypoints = [Link]([3, 8, 1, 10])
[Link](ypoints, marker = 'o')
[Link]()
import [Link] as plt
import numpy as np
ypoints = [Link]([3, 8, 1, 10])
[Link](ypoints, linestyle = 'dotted')
[Link]()
import [Link] as plt
import numpy as np
ypoints = [Link]([3, 8, 1, 10])
[Link](ypoints, color = 'r')
[Link]()
import [Link] as plt
import numpy as np
ypoints = [Link]([3, 8, 1, 10])
[Link](ypoints, marker = 'o', ms = 20, mec = 'r')
[Link]()
8. Line plot
# importing matplotlib module
from matplotlib import pyplot as plt
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 2]
# Function to plot
[Link](x, y)
# function to show the plot
[Link]()
9. Bar Plot
# importing matplotlib module
from matplotlib import pyplot as plt
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 2]
# Function to plot
[Link](x, y)
# function to show the plot
[Link]()
10. Scatter Plot
# importing matplotlib module
from matplotlib import pyplot as plt
# x-axis values
x = [5, 2, 9, 4, 7, 3, 8, 10, 1]
# Y-axis values
y = [10, 5, 8, 4, 2, 1, 6, 3, 9]
# Function to plot scatter
[Link](x, y)
# function to show the plot
[Link]()
11. Legends, labels and titles
# Ticks are the markers denoting data points on axes.
# importing libraries
import [Link] as plt
import numpy as np
# values of x and y axes
x = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
y = [1, 4, 3, 2, 7, 6, 9, 8, 10, 5]
[Link](x, y, 'g')
[Link]('x')
[Link]('y')
# here we set the size for ticks, rotation and color value
plt.tick_params(axis="x", labelsize=18, labelrotation=-60, labelcolor="blue")
plt.tick_params(axis="y", labelsize=12, labelrotation=20, labelcolor="black")
[Link]()
12. Adding title and Labelling the Axes in the graph
# importing matplotlib module
from matplotlib import pyplot as plt
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 2]
# Function to plot
[Link](x, y)
# Adding Title
[Link]("Bar graph ")
# Labeling the axes
[Link]("Time (hr)")
[Link]("Position (Km)")
# function to show the plot
[Link]()
13. Adding Legend in the graph
# importing modules
import numpy as np
import [Link] as plt
# Y-axis values
y1 = [2, 3, 4.5]
# Y-axis values
y2 = [1, 1.5, 5]
# Function to plot
[Link](y1)
[Link](y2)
# Function add a legend
[Link](["blue", "green"], loc ="lower right")
# function to show the plot
[Link]()
#subplots
import [Link] as plt
import numpy as np
#plot 1:
x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])
[Link](1, 2, 1)
[Link](x,y)
#plot 2:
x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])
[Link](1, 2, 2)
[Link](x,y)
[Link]()
14. Histogram
import [Link] as plt
import numpy as np
data = [Link](170, 10, 250)
#The hist() function will read the array and produce a histogram:
#set the bin value 30
[Link](data);
[Link]()
[Link](data, bins=30);
[Link]()
#import [Link]
import [Link] as plt
import numpy as np
#draw plot as fig
fig, ax = [Link]()
x = [Link](0, 20, 1000)
[Link](x, [Link](x))
[Link]('equal')
#text annotation to the plot where it indicate maximum value of the curv
[Link]('local maximum', xy=(6.28, 1), xytext=(10, 4),
arrowprops=dict(facecolor='black', shrink=0.05))
#text annotation to the plot where it indicate minimum value of the curv
[Link]('local minimum', xy=(5 * [Link], -1), xytext=(2, -6),
arrowprops=dict(facecolor='black', shrink=0.05));

# angleA : starting angle of the path


# angleB : ending angle of the path
# armA : length of the starting arm
# armB : length of the ending arm
# rad : rounding radius of the edges
# connect(posA, posB)
15. 3D-Plot
import numpy as np
import [Link] as plt
fig = [Link]() #USE TO CREATE A NEW FIG
ax = [Link](projection ='3d')
# importing mplot3d toolkits, numpy and matplotlib
from mpl_toolkits import mplot3d
import numpy as np
import [Link] as plt
fig = [Link]()
# syntax for 3-D projection
ax = [Link](projection ='3d')
# defining all 3 axes
z = [Link](0, 1, 1000)
x = z * [Link](25 * z)
y = z * [Link](25 * z)
# plotting
ax.plot3D(x, y, z, 'green')
ax.set_title('3D line plot')
[Link]()
16. Pie Charts
# Import libraries
from matplotlib import pyplot as plt
import numpy as np
# Creating dataset
cars = ['AUDI', 'BMW', 'FORD',
'TESLA', 'JAGUAR', 'MERCEDES']
data = [23, 17, 35, 29, 12, 41]
# Creating plot
fig = [Link](figsize =(10, 7))
[Link](data, labels = cars)
# show plot
[Link]()
#import numpy
import numpy as np
from matplotlib import pyplot as plt
x = [Link](1,11)
y=2*x+5
[Link]("Matplotlib demo")
[Link]("x axis caption")
[Link]("y axis caption")
[Link](x,y)
[Link]()
import numpy as np
import [Link] as plt
# Compute the x and y coordinates for points on a sine curve
x = [Link](0, 3 * [Link], 0.1)
print(x)
y = [Link](x)
[Link]("sine wave form")
# Plot the points using matplotlib
[Link](x, y)
[Link]()
17. Logarithmic Axes in Matplotlib
import [Link] as plt
# exponential function y = 10^x
data = [10**i for i in range(5)]
[Link](data)
import [Link] as plt
# exponential function y = 10^x
data = [10**i for i in range(5)]
# convert y-axis to Logarithmic scale
[Link]("log")
[Link](data)
import [Link] as plt
# exponential function x = 10^y
datax = [ 10**i for i in range(5)]
datay = [ i for i in range(5)]
#convert x-axis to Logarithmic scale
[Link]("log")
[Link](datax,datay)

You might also like