100% found this document useful (1 vote)
7K views7 pages

Hands-On Data Visualization with Matplotlib

The document provides code examples for creating various data visualizations using Matplotlib in Python. It includes examples for plotting simple and multi-line graphs, scatter plots, bar plots comparing iris species attributes, and histograms. The code demonstrates basic Matplotlib functionality like customizing titles, labels, legends, and saving figures. Multiple tasks are presented with increasing complexity to help learners build skills in data visualization.

Uploaded by

Vannila
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
100% found this document useful (1 vote)
7K views7 pages

Hands-On Data Visualization with Matplotlib

The document provides code examples for creating various data visualizations using Matplotlib in Python. It includes examples for plotting simple and multi-line graphs, scatter plots, bar plots comparing iris species attributes, and histograms. The code demonstrates basic Matplotlib functionality like customizing titles, labels, legends, and saving figures. Multiple tasks are presented with increasing complexity to help learners build skills in data visualization.

Uploaded by

Vannila
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

Hands on Data Visualization

Hands on 1:-

import matplotlib
[Link]('Agg')
import [Link] as plt
import numpy as np
import [Link] as gridspec
#Write your code here
def test_my_first_plot():
fig=[Link](figsize=(8,6))
ax=fig.add_subplot(111)
t=[5,10,15,20,25]
d=[25, 50, 75, 100, 125]
[Link](t,d,label='d=5t')
[Link](title="Time vs Distance Covered",xlabel="time (seconds)",
ylabel="distance (meters)", xlim=(0,30), ylim=(0,130))
[Link]()
[Link]('[Link]')
[Link]()

m
test_my_first_plot()

er as
co
Run -> python3 [Link] | tee [Link]

eH w
================================================================================

o.
==================================================================
rs e
ou urc
Hands on 2:-

import matplotlib
[Link]('Agg')
o

import [Link] as plt


aC s

import numpy as np
v i y re

import [Link] as gridspec


#Write your code here

************************************ Task 1
***************************************
ed d
ar stu

import [Link] as ticker


def test_sine_wave_plot():
fig=[Link](figsize=(12,3))
ax=fig.add_subplot(111)
t=[Link](0.0,2.0,200)
sh is

v=[Link](2.5*[Link]*t)
[Link](t,v,label='sin(t)', color='red')
Th

[Link](title="Sine Wave",xlabel="Time (seconds)", ylabel="Voltage (mV)",


xlim=(0,2), ylim=(-1,1))
[Link]([0,0.2,0.4,0.6,0.8,1.0,1.2,1.4,1.6,1.8,2.0])
[Link]([-1,0,1])
[Link](linestyle='--', which='major')
[Link]()
[Link]()
[Link]("[Link]")

test_sine_wave_plot()

********************************** Task 2
******************************************

def test_multi_curve_plot():
fig=[Link](figsize=(12,3))

This study source was downloaded by 100000823840190 from [Link] on 12-10-2021 00:08:43 GMT -06:00

[Link]
ax=fig.add_subplot(111)
x=[Link](0.0,5.0,20)
y1=x
y2=x**2
y3=x**3
[Link](title="Linear, Quadratic, & Cubic Equations", xlabel="X",
ylabel="f(X)")
[Link](x, y1, label="y = x", color="red", marker="o")
[Link](x, y2, label ="y = x**2", color ="green", marker="s")
[Link](x, y3, label="y = x**3", color="blue", marker="^")
[Link]()
[Link]()
[Link]("[Link]")

test_multi_curve_plot()

****************************************** Task 3
*****************************************
def test_scatter_plot():
fig=[Link](figsize=(12,3))
ax=fig.add_subplot(111)
s=[50, 60, 55, 50, 70, 65, 75, 65, 80, 90, 93, 95]

m
months = [1,2,3,4,5,6,7,8,9,10,11,12]

er as
[Link](months, s, color="red" , marker = "o")

co
[Link](title="Cars Sold by Company 'X' in 2017", xlabel="Months", ylabel="No.

eH w
of Cars Sold", xlim=(0,13), ylim=(20,100))
ax.set_xticklabels(['Jan','Mar','May','Jul','Sep','Nov'])

o.
[Link]([1,3,5,7,9,11]) rs e
[Link]()
ou urc
[Link]("[Link]")

test_scatter_plot()
o

Run-> python2 [Link] | [Link], [Link], [Link]


aC s

================================================================================
v i y re

====================================================

Hands on 3:-
import matplotlib
[Link]('Agg')
ed d

import [Link] as plt


ar stu

import numpy as np
import [Link] as gridspec
#Write your code here
******************************** Task 1 ***********************************
def test_barplot_of_iris_sepal_length():
sh is

fig=[Link](figsize=(8,6))
ax=fig.add_subplot(111)
Th

species = ['setosa','versicolor','viriginica']
species_index=[0.2, 1.2, 2.2]
sepal_len =[5.01, 5.94, 6.59]
[Link](species_index, sepal_len, color="red", width=0.5, edgecolor="black")
[Link](xlabel="Species", ylabel="Sepal Length (cm)", title = "Mean Sepal
Length of Iris Species", xlim=(0,3), ylim=(0,7))
ax.set_xticks([0.45, 1.45, 2.45])
ax.set_xticklabels(['setosa','versicolor','viriginica'])
[Link]()
[Link]("bar_iris_sepal.png")

test_barplot_of_iris_sepal_length()

********************************************* Task 2
****************************************
def test_barplot_of_iris_measurements():

This study source was downloaded by 100000823840190 from [Link] on 12-10-2021 00:08:43 GMT -06:00

[Link]
fig=[Link](figsize=(8,6))
ax=fig.add_subplot(111)
sepal_len=[5.01,5.94,6.59]
sepal_wd=[3.42,2.77,2.97]
petal_len = [1.46, 4.26, 5.55]
petal_wd=[0.24, 1.33, 2.03]
species=['setosa','versicolor','viriginica']
species_index1=[0.7, 1.7, 2.7]
species_index2=[0.9,1.9,2.9]
species_index3=[1.1,2.1,3.1]
species_index4=[1.3, 2.3, 3.3]

[Link](species_index1, sepal_len, label = "Sepal Length" , color="c",


width=0.2, edgecolor="black")
[Link](species_index2, sepal_wd, label = "Sepal Width" , color="m", width=0.2,
edgecolor="black")
[Link](species_index3, petal_len, label = "Petal Length" , color="y",
width=0.2, edgecolor="black")
[Link](species_index4, petal_wd, label= "Petal Width", color="orange",
width=0.2, edgecolor="black")

[Link](xlabel="Species", ylabel="Iris Measurements (cm)", title="Mean

m
Measurements of Iris Species", xlim=(0.5, 3.7), ylim=(0,10))

er as
ax.set_xticks([1.1, 2.1, 3.1])

co
ax.set_xticklabels(['setosa', 'versicolor','viriginica'])

eH w
[Link]()
[Link]()

o.
[Link]("bar_iris_measure.png")
rs e
ou urc
test_barplot_of_iris_measurements()

****************************************** Task 3
***********************************
o

def test_hbarplot_of_iris_petal_length():
aC s

fig=[Link](figsize=(12,5))
v i y re

ax=fig.add_subplot(111)
species=['setosa','versicolor','viriginica']
species_index=[0.2, 1.2, 2.2]
petal_len=[1.46, 4.26, 5.55]
[Link](species_index, petal_len, height=0.5, color='c', edgecolor='black')
ed d

[Link](ylabel="Species", xlabel="Petal Length (cm)", title = "Mean Petal


ar stu

Length of Iris Species")


ax.set_yticks([0.45, 1.45, 2.45])
ax.set_yticklabels(['setosa','versicolor','viriginica'])
[Link]()
[Link]("bar_iris_petal.png")
sh is

test_hbarplot_of_iris_petal_length()
Th

Run -> python3 [Link]

================================================================================
===========================================================

Hands on 4 :-

import matplotlib
[Link]('Agg')
import [Link] as plt
import numpy as np
import [Link] as gridspec
#Write your code here
**************************** Task 1 **********************************
def test_hist_of_a_sample_normal_distribution():

This study source was downloaded by 100000823840190 from [Link] on 12-10-2021 00:08:43 GMT -06:00

[Link]
fig = [Link](figsize=(8,6))
ax=fig.add_subplot(111)
[Link](100)
x1=25 + 3.0*([Link](1000))
[Link](x1, bins=30)
[Link](xlabel="X1", ylabel="Bin Count", title="Histogram of a Single Dataset")
[Link]()
[Link]("histogram_normal.png")
test_hist_of_a_sample_normal_distribution()

********************************************** Task 2
************************************

def test_boxplot_of_four_normal_distribution():
fig = [Link](figsize=(8,6))
ax=fig.add_subplot(111)
[Link](100)
x1=25+3.0*([Link](1000))
x2=35+5.0*([Link](1000))
x3=55+10.0*([Link](1000))
x4=45+3.0*([Link](1000))

m
labels = ['X1','X2','X3','X4']

er as
co
[Link]([x1, x2, x3, x4], labels = labels , notch=True, sym='+' ,

eH w
patch_artist=True)
[Link](xlabel='Dataset', ylabel = "Value", title = "Box plot of Multiple

o.
Datasets") rs e
[Link]()
ou urc
[Link]("box_distribution.png")
test_boxplot_of_four_normal_distribution()
o

Run -> python3 [Link]


aC s
v i y re

================================================================================
================================

Hands on 5 :-
ed d

import matplotlib
ar stu

[Link]('Agg')
import [Link] as plt
import numpy as np
import [Link] as gridspec
#Write your code here
sh is

************************************** Task 1 ************************


Th

def test_generate_plot_with_style1():
with [Link]('ggplot'):
fig=[Link](figsize=(8,6))
ax=fig.add_subplot(111)
sepal_len=[5.01,5.94,6.59]
sepal_wd=[3.42,2.77,2.97]
petal_len = [1.46, 4.26, 5.55]
petal_wd=[0.24, 1.33, 2.03]
species=['setosa','versicolor','viriginica']
species_index1=[0.7, 1.7, 2.7]
species_index2=[0.9,1.9,2.9]
species_index3=[1.1,2.1,3.1]
species_index4=[1.3, 2.3, 3.3]

[Link](species_index1, sepal_len, label = "Sepal Length", width=0.2)


[Link](species_index2, sepal_wd, label = "Sepal Width", width=0.2)

This study source was downloaded by 100000823840190 from [Link] on 12-10-2021 00:08:43 GMT -06:00

[Link]
[Link](species_index3, petal_len, label = "Petal Length", width=0.2)
[Link](species_index4, petal_wd, label= "Petal Width", width=0.2)

[Link](xlabel="Species", ylabel="Iris Measurements (cm)", title="Mean


Measurements of Iris Species", xlim=(0.5, 3.7), ylim=(0,10))
ax.set_xticks([1.1, 2.1, 3.1])
ax.set_xticklabels(['setosa', 'versicolor','viriginica'])
[Link]()
[Link]()
[Link]("[Link]")
test_generate_plot_with_style1()

************************************** Task 2
***********************************************
def test_generate_plot_with_style2():
with [Link]('seaborn-colorblind'):
fig=[Link](figsize=(8,6))
ax=fig.add_subplot(111)
sepal_len=[5.01,5.94,6.59]
sepal_wd=[3.42,2.77,2.97]
petal_len = [1.46, 4.26, 5.55]
petal_wd=[0.24, 1.33, 2.03]

m
species=['setosa','versicolor','viriginica']

er as
species_index1=[0.7, 1.7, 2.7]

co
species_index2=[0.9,1.9,2.9]

eH w
species_index3=[1.1,2.1,3.1]
species_index4=[1.3, 2.3, 3.3]

o.
[Link](species_index1,
rs e sepal_len, label = "Sepal Length", width=0.2)
ou urc
[Link](species_index2, sepal_wd, label = "Sepal Width", width=0.2)
[Link](species_index3, petal_len, label = "Petal Length", width=0.2)
[Link](species_index4, petal_wd, label= "Petal Width", width=0.2)
o

[Link](xlabel="Species", ylabel="Iris Measurements (cm)", title="Mean


aC s

Measurements of Iris Species", xlim=(0.5, 3.7), ylim=(0,10))


v i y re

ax.set_xticks([1.1, 2.1, 3.1])


ax.set_xticklabels(['setosa', 'versicolor','viriginica'])
[Link]()
[Link]()
[Link]("[Link]")
ed d

test_generate_plot_with_style2()
ar stu

********************************* Task 3 ***********************************


def test_generate_plot_with_style3():
with [Link]('grayscale'):
fig=[Link](figsize=(8,6))
sh is

ax=fig.add_subplot(111)
sepal_len=[5.01,5.94,6.59]
Th

sepal_wd=[3.42,2.77,2.97]
petal_len = [1.46, 4.26, 5.55]
petal_wd=[0.24, 1.33, 2.03]
species=['setosa','versicolor','viriginica']
species_index1=[0.7, 1.7, 2.7]
species_index2=[0.9,1.9,2.9]
species_index3=[1.1,2.1,3.1]
species_index4=[1.3, 2.3, 3.3]

[Link](species_index1, sepal_len, label = "Sepal Length", width=0.2)


[Link](species_index2, sepal_wd, label = "Sepal Width", width=0.2)
[Link](species_index3, petal_len, label = "Petal Length", width=0.2)
[Link](species_index4, petal_wd, label= "Petal Width", width=0.2)

[Link](xlabel="Species", ylabel="Iris Measurements (cm)", title="Mean


Measurements of Iris Species", xlim=(0.5, 3.7), ylim=(0,10))

This study source was downloaded by 100000823840190 from [Link] on 12-10-2021 00:08:43 GMT -06:00

[Link]
ax.set_xticks([1.1, 2.1, 3.1])
ax.set_xticklabels(['setosa', 'versicolor','viriginica'])
[Link]()
[Link]()
[Link]("[Link]")
test_generate_plot_with_style3()

Run->python3 [Link]
================================================================================
================================================================================
========

Hands on 6:-

import matplotlib
[Link]('Agg')
import [Link] as plt
import numpy as np
import [Link] as gridspec
#Write your code here

**************************************** Task 1

m
************************************************************************

er as
def test_generate_figure1():

co
t=[Link](0.0,5.0,0.01)

eH w
s1=[Link](2*[Link]*t)
s2=[Link](4*[Link]*t)

o.
fig=[Link](figsize=(8,6)) rs e
axes1=[Link](2,1,1,title='Sin(2*pi*x)')
ou urc
[Link](t,s1)
axes2=[Link](2,1,2, title='Sin(4*pi*x)', sharex=axes1, sharey=axes1)
[Link](t,s2)
[Link]()
o

[Link]('[Link]')
aC s

test_generate_figure1()
v i y re

****************************************** Task 2
********************************************************************
def test_generate_figure2():
[Link](1000)
ed d

x=[Link](10)
ar stu

y=[Link](10)
z=[Link](x**2+y**2)
fig=[Link](figsize=(8,6))
axes1=[Link](2,2,1, title='Scatter plot with Upper Traingle Markers')
[Link](x, y, s=80, c=z, marker='^')
sh is

axes1.set_xticks([0.0, 0.4, 0.8, 1.2])


axes1.set_yticks([-0.2, 0.2, 0.6, 1.0])
Th

axes2=[Link](2,2,2, title="Scatter plot with Plus Markers")


[Link](x, y, s=80, c=z, marker='+')
axes2.set_xticks([0.0, 0.4, 0.8, 1.2])
axes2.set_yticks([-0.2, 0.2, 0.6, 1.0])

axes3=[Link](2,2,3, title="Scatter plot with Circle Markers")


[Link](x,y, s=80, c=z, marker='o')
axes3.set_xticks([0.0, 0.4, 0.8, 1.2])
axes3.set_yticks([-0.2, 0.2, 0.6, 1.0])

axes4=[Link](2,2,4, title="Scatter plot with Diamond Markers")


[Link](x,y, s=80, c=z, marker='d')
axes4.set_xticks([0.0, 0.4, 0.8, 1.2])
axes4.set_yticks([-0.2, 0.2, 0.6, 1.0])

This study source was downloaded by 100000823840190 from [Link] on 12-10-2021 00:08:43 GMT -06:00

[Link]
plt.tight_layout()
[Link]()
[Link]('[Link]')

test_generate_figure2()

*************************************** Task 3
***************************************************************

def test_generate_figure3():
x=[Link](1,101)
y1=x
y2=x**2
y3=x**3

fig=[Link](figsize=(8,6))
g=[Link](2,2)
axes1=[Link](g[0,0],title='y = x')
[Link](x,y1)

axes2=[Link](g[1,0],title='y = x**2')
[Link](x,y2)

m
er as
axes3=[Link](g[:,1], title='y = x**3')

co
[Link](x,y3)

eH w
plt.tight_layout()

o.
[Link]() rs e
[Link]('[Link]')
ou urc
test_generate_figure3()

================================================================================
o

=============================================================
aC s
v i y re
ed d
ar stu
sh is
Th

This study source was downloaded by 100000823840190 from [Link] on 12-10-2021 00:08:43 GMT -06:00

[Link]
Powered by TCPDF ([Link])

Hands on Data Visualization
Hands on 1:-
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import num
ax=fig.add_subplot(111)
  x=np.linspace(0.0,5.0,20)
  y1=x
  y2=x**2
  y3=x**3
  ax.set(title="Linear, Quadratic, & Cubic E
fig=plt.figure(figsize=(8,6))
  ax=fig.add_subplot(111)
  sepal_len=[5.01,5.94,6.59]
  sepal_wd=[3.42,2.77,2.97]
  petal_le
fig = plt.figure(figsize=(8,6))
  ax=fig.add_subplot(111)
  np.random.seed(100)
  x1=25 + 3.0*(np.random.randn(1000))
  ax.
ax.bar(species_index3, petal_len, label = "Petal Length", width=0.2)
    ax.bar(species_index4, petal_wd, label= "Petal W
ax.set_xticks([1.1, 2.1, 3.1])
    ax.set_xticklabels(['setosa', 'versicolor','viriginica'])
    plt.legend()
    plt.sho
plt.tight_layout()
  plt.show()
  plt.savefig('testfigure2.png')
test_generate_figure2()
**********************************

You might also like