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

Mat Plot Lib

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)
3 views7 pages

Mat Plot Lib

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
You are on page 1/ 7

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 matplotlib.pyplot as plt
# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]
# plotting the points
plt.plot(x, y)
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('My first graph!')
# function to show the plot
plt.show()
import numpy as np
x = np.arange(0, 10, 0.1)
y = np.sin(x)

# figures in matplotlib using figure()


plt.figure(figsize=(10, 8))
plt.savefig("test.png")
plt.plot(x, y)
plt.show()
7. subplots()
#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)
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, linestyle = 'dotted')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, color = 'r')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o', ms = 20, mec = 'r')
plt.show()
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
plt.plot(x, y)
# function to show the plot
plt.show()
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
plt.bar(x, y)
# function to show the plot
plt.show()
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
plt.scatter(x, y)
# function to show the plot
plt.show()
11. Legends, labels and titles
# Ticks are the markers denoting data points on axes.
# importing libraries
import matplotlib.pyplot 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]
plt.plot(x, y, 'g')
plt.xlabel('x')
plt.ylabel('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")
plt.show()
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
plt.bar(x, y)
# Adding Title
plt.title("Bar graph ")
# Labeling the axes
plt.xlabel("Time (hr)")
plt.ylabel("Position (Km)")
# function to show the plot
plt.show()
13. Adding Legend in the graph
# importing modules
import numpy as np
import matplotlib.pyplot as plt
# Y-axis values
y1 = [2, 3, 4.5]
# Y-axis values
y2 = [1, 1.5, 5]
# Function to plot
plt.plot(y1)
plt.plot(y2)
# Function add a legend
plt.legend(["blue", "green"], loc ="lower right")
# function to show the plot
plt.show()
#subplots
import matplotlib.pyplot as plt
import numpy as np
#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()
14. Histogram
import matplotlib.pyplot as plt
import numpy as np
data = np.random.normal(170, 10, 250)
#The hist() function will read the array and produce a histogram:
#set the bin value 30
plt.hist(data);
plt.show()
plt.hist(data, bins=30);
plt.show()
#import matplotlib.pyplot
import matplotlib.pyplot as plt
import numpy as np
#draw plot as fig
fig, ax = plt.subplots()
x = np.linspace(0, 20, 1000)
ax.plot(x, np.cos(x))
ax.axis('equal')
#text annotation to the plot where it indicate maximum value of the curv
ax.annotate('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
ax.annotate('local minimum', xy=(5 * np.pi, -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 matplotlib.pyplot as plt
fig = plt.figure() #USE TO CREATE A NEW FIG
ax = plt.axes(projection ='3d')
# importing mplot3d toolkits, numpy and matplotlib
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
# syntax for 3-D projection
ax = plt.axes(projection ='3d')
# defining all 3 axes
z = np.linspace(0, 1, 1000)
x = z * np.sin(25 * z)
y = z * np.cos(25 * z)
# plotting
ax.plot3D(x, y, z, 'green')
ax.set_title('3D line plot')
plt.show()
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 = plt.figure(figsize =(10, 7))
plt.pie(data, labels = cars)
# show plot
plt.show()
#import numpy
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1,11)
y=2*x+5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
print(x)
y = np.sin(x)
plt.title("sine wave form")
# Plot the points using matplotlib
plt.plot(x, y)
plt.show()
17. Logarithmic Axes in Matplotlib
import matplotlib.pyplot as plt
# exponential function y = 10^x
data = [10**i for i in range(5)]
plt.plot(data)
import matplotlib.pyplot as plt
# exponential function y = 10^x
data = [10**i for i in range(5)]
# convert y-axis to Logarithmic scale
plt.yscale("log")
plt.plot(data)
import matplotlib.pyplot 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
plt.xscale("log")
plt.plot(datax,datay)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy