LAB MANUAL:
Week 13: Intro to AI
April 21,
2024
Week
13:
Lab 1: Data Visualization by Matplotlib
14
Lab 1: Data
Visualization by
Matplotlib.
Data Visualization in Python:
Data visualization in Python refers to the process of representing data
graphically using Python libraries such as Matplotlib, Seaborn, Plotly,
and others. By creating visual representations such as charts, graphs,
and plots, data visualization allows individuals and organizations to gain
insights from data, identify patterns, trends, and outliers, and
communicate findings effectively.
Through data visualization, complex datasets can be transformed into
easily understandable visual formats, enabling stakeholders to make
informed decisions based on the insights gained from the visual
representations. Python's data visualization tools provide a wide range
of options for creating various types of visualizations, including bar
charts, line plots, scatter plots, histograms, heatmaps, and more,
catering to different data analysis needs and preferences. 16
Matplotlib in Python:
Matplotlib is a popular Python library used for creating static, animated,
and interactive visualizations in Python. It provides a wide range of
plotting tools and features, making it suitable for various types of data
visualization, including line plots, scatter plots, bar plots, histograms,
and more. Matplotlib is often used in combination with other libraries
such as NumPy and Pandas for data manipulation and analysis. It offers
a high level of customization for creating publication-quality figures and
supports various output formats, including PNG, PDF, SVG, and more.
Overall, Matplotlib is a powerful tool for data visualization in Python.
16
Matplotlib in Python:
#Import Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#Create 200 data
x=np.linspace(1,20,200)
y=y=np.sin(x)
#Plot the line grap
plt.plot(x,y)
16
Matplotlib in Python:
#X-label, Y-label and title of the graph can be added
plt.plot(x,y)
plt.xlabel('This is my X data')
plt.ylabel('This is my Y data')
plt.title('This is my title')
16
Matplotlib in Python:
plt.plot(x,y,'--b')
plt.xlabel('X')
plt.ylabel('This is my y value')
plt.title('X vs. Y')
16
Matplotlib in Python:
#Draw a scatter plot
a=np.random.rand(50)
b=np.random.rand(50)
plt.scatter(a,b)
16
Matplotlib in Python:
#Draw a scatter plot with fixed colour
x=np.random.rand(50)
y=np.random.rand(50)
plt.scatter(x,y,c=‘r’)
plt.show()
16
Matplotlib in Python:
#Draw a scatter plot with different size
x=np.random.rand(50)
y=np.random.rand(50)
colours=np.random.rand(50)
size=100*(np.random.rand(50))
plt.scatter(x,y,c=colours,s=size)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('X vs Y')
plt.show()
16
Matplotlib in Python:
#Draw a scatter plot with colour concentration
x=np.random.rand(50)
y=np.random.rand(50)
colours=np.random.rand(50)
size=100*(np.random.rand(50))
plt.scatter(x,y,c=colours,s=size,alpha=0.5)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('X vs Y')
plt.show()
16
Matplotlib in Python:
#Draw a bar graph
x=['a','b','c','d','e']
y=np.random.rand(5)
plt.bar(x,y)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('X vs Y')
plt.show()
16
Matplotlib in Python:
#Draw a horizontal bar graph
x=['a','b','c','d','e']
y=np.random.rand(5)
plt.barh(x,y)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('X vs Y')
plt.show()
16
Matplotlib in Python:
#How can we change the size of the picture
x=np.linspace(1,20,200)
y=np.sin(x)
plt.figure(figsize=(8,5))
plt.plot(x,y,'--b')
plt.xlabel('This is my X data')
plt.ylabel('This is my Y data')
plt.title('This is my title')
plt.show()
16
Matplotlib in Python:
#How to change the shape
x=np.random.rand(50)
y=np.random.rand(50)
colours=np.random.rand(50)
size=100*(np.random.rand(50))
plt.scatter(x,y,c=colours,s=size,marker='D')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('X vs Y')
plt.show()
16
Matplotlib in Python:
#How to change the shape
x=[4,5,6,7,8]
y=[6,7,8,9,10]
plt.scatter(x,y,marker='*')
16
Matplotlib in Python:
#How to draw histogram
data=[1,2,3,4,5,1,1,1,2,2,3,3,4,5,5,6,4]
plt.hist(data)
16
Matplotlib in Python:
#How to give user input colour
x=[4,5,6,7,8]
y=[60,17,28,96,10]
colour=['red','blue','yellow','green','black']
plt.scatter(x,y,c=colour,marker='*')
16
Matplotlib in Python:
#How to make a 3D graph
x=np.random.rand(50)
y=np.random.rand(50)
z=np.random.rand(50)
fig=plt.figure()
ax=fig.add_subplot(projection='3d')
ax.scatter(x,y,z)
16
Thank You