Hands On Data Visualization Using Matplotlib
Hands On Data Visualization Using Matplotlib
Hands on 1:-
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
#Write your code here
def test_my_first_plot():
fig=plt.figure(figsize=(8,6))
ax=fig.add_subplot(111)
t=[5,10,15,20,25]
d=[25, 50, 75, 100, 125]
plt.plot(t,d,label='d=5t')
ax.set(title="Time vs Distance Covered",xlabel="time (seconds)",
ylabel="distance (meters)", xlim=(0,30), ylim=(0,130))
plt.legend()
plt.savefig('scatter.png')
plt.show()
m
test_my_first_plot()
er as
co
Run -> python3 prog.py | tee scatter.png
eH w
================================================================================
o.
==================================================================
rs e
ou urc
Hands on 2:-
import matplotlib
matplotlib.use('Agg')
o
import numpy as np
v i y re
************************************ Task 1
***************************************
ed d
ar stu
v=np.sin(2.5*np.pi*t)
plt.plot(t,v,label='sin(t)', color='red')
Th
test_sine_wave_plot()
********************************** Task 2
******************************************
def test_multi_curve_plot():
fig=plt.figure(figsize=(12,3))
This study source was downloaded by 100000823840190 from CourseHero.com on 12-10-2021 00:08:43 GMT -06:00
https://www.coursehero.com/file/85760630/Hands-on-Data-Visualization-using-matplotlibtxt/
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 Equations", xlabel="X",
ylabel="f(X)")
ax.plot(x, y1, label="y = x", color="red", marker="o")
ax.plot(x, y2, label ="y = x**2", color ="green", marker="s")
ax.plot(x, y3, label="y = x**3", color="blue", marker="^")
plt.legend()
plt.show()
plt.savefig("multicurve.png")
test_multi_curve_plot()
****************************************** Task 3
*****************************************
def test_scatter_plot():
fig=plt.figure(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
ax.scatter(months, s, color="red" , marker = "o")
co
ax.set(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.
plt.xticks([1,3,5,7,9,11]) rs e
plt.show()
ou urc
plt.savefig("scatter.png")
test_scatter_plot()
o
================================================================================
v i y re
====================================================
Hands on 3:-
import matplotlib
matplotlib.use('Agg')
ed d
import numpy as np
import matplotlib.gridspec as gridspec
#Write your code here
******************************** Task 1 ***********************************
def test_barplot_of_iris_sepal_length():
sh is
fig=plt.figure(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]
ax.bar(species_index, sepal_len, color="red", width=0.5, edgecolor="black")
ax.set(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'])
plt.show()
plt.savefig("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 CourseHero.com on 12-10-2021 00:08:43 GMT -06:00
https://www.coursehero.com/file/85760630/Hands-on-Data-Visualization-using-matplotlibtxt/
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_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]
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
plt.legend()
plt.show()
o.
plt.savefig("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=plt.figure(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]
ax.barh(species_index, petal_len, height=0.5, color='c', edgecolor='black')
ed d
test_hbarplot_of_iris_petal_length()
Th
================================================================================
===========================================================
Hands on 4 :-
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
#Write your code here
**************************** Task 1 **********************************
def test_hist_of_a_sample_normal_distribution():
This study source was downloaded by 100000823840190 from CourseHero.com on 12-10-2021 00:08:43 GMT -06:00
https://www.coursehero.com/file/85760630/Hands-on-Data-Visualization-using-matplotlibtxt/
fig = plt.figure(figsize=(8,6))
ax=fig.add_subplot(111)
np.random.seed(100)
x1=25 + 3.0*(np.random.randn(1000))
ax.hist(x1, bins=30)
ax.set(xlabel="X1", ylabel="Bin Count", title="Histogram of a Single Dataset")
plt.show()
plt.savefig("histogram_normal.png")
test_hist_of_a_sample_normal_distribution()
********************************************** Task 2
************************************
def test_boxplot_of_four_normal_distribution():
fig = plt.figure(figsize=(8,6))
ax=fig.add_subplot(111)
np.random.seed(100)
x1=25+3.0*(np.random.randn(1000))
x2=35+5.0*(np.random.randn(1000))
x3=55+10.0*(np.random.randn(1000))
x4=45+3.0*(np.random.randn(1000))
m
labels = ['X1','X2','X3','X4']
er as
co
ax.boxplot([x1, x2, x3, x4], labels = labels , notch=True, sym='+' ,
eH w
patch_artist=True)
ax.set(xlabel='Dataset', ylabel = "Value", title = "Box plot of Multiple
o.
Datasets") rs e
plt.show()
ou urc
plt.savefig("box_distribution.png")
test_boxplot_of_four_normal_distribution()
o
================================================================================
================================
Hands on 5 :-
ed d
import matplotlib
ar stu
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
#Write your code here
sh is
def test_generate_plot_with_style1():
with plt.style.context('ggplot'):
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_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]
This study source was downloaded by 100000823840190 from CourseHero.com on 12-10-2021 00:08:43 GMT -06:00
https://www.coursehero.com/file/85760630/Hands-on-Data-Visualization-using-matplotlibtxt/
ax.bar(species_index3, petal_len, label = "Petal Length", width=0.2)
ax.bar(species_index4, petal_wd, label= "Petal Width", width=0.2)
************************************** Task 2
***********************************************
def test_generate_plot_with_style2():
with plt.style.context('seaborn-colorblind'):
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_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.
ax.bar(species_index1,
rs e sepal_len, label = "Sepal Length", width=0.2)
ou urc
ax.bar(species_index2, sepal_wd, label = "Sepal Width", width=0.2)
ax.bar(species_index3, petal_len, label = "Petal Length", width=0.2)
ax.bar(species_index4, petal_wd, label= "Petal Width", width=0.2)
o
test_generate_plot_with_style2()
ar stu
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]
This study source was downloaded by 100000823840190 from CourseHero.com on 12-10-2021 00:08:43 GMT -06:00
https://www.coursehero.com/file/85760630/Hands-on-Data-Visualization-using-matplotlibtxt/
ax.set_xticks([1.1, 2.1, 3.1])
ax.set_xticklabels(['setosa', 'versicolor','viriginica'])
plt.legend()
plt.show()
plt.savefig("plotstyle3.png")
test_generate_plot_with_style3()
Run->python3 prog.py
================================================================================
================================================================================
========
Hands on 6:-
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
#Write your code here
**************************************** Task 1
m
************************************************************************
er as
def test_generate_figure1():
co
t=np.arange(0.0,5.0,0.01)
eH w
s1=np.sin(2*np.pi*t)
s2=np.sin(4*np.pi*t)
o.
fig=plt.figure(figsize=(8,6)) rs e
axes1=plt.subplot(2,1,1,title='Sin(2*pi*x)')
ou urc
axes1.plot(t,s1)
axes2=plt.subplot(2,1,2, title='Sin(4*pi*x)', sharex=axes1, sharey=axes1)
axes2.plot(t,s2)
plt.show()
o
plt.savefig('testfigure1.png')
aC s
test_generate_figure1()
v i y re
****************************************** Task 2
********************************************************************
def test_generate_figure2():
np.random.seed(1000)
ed d
x=np.random.rand(10)
ar stu
y=np.random.rand(10)
z=np.sqrt(x**2+y**2)
fig=plt.figure(figsize=(8,6))
axes1=plt.subplot(2,2,1, title='Scatter plot with Upper Traingle Markers')
axes1.scatter(x, y, s=80, c=z, marker='^')
sh is
This study source was downloaded by 100000823840190 from CourseHero.com on 12-10-2021 00:08:43 GMT -06:00
https://www.coursehero.com/file/85760630/Hands-on-Data-Visualization-using-matplotlibtxt/
plt.tight_layout()
plt.show()
plt.savefig('testfigure2.png')
test_generate_figure2()
*************************************** Task 3
***************************************************************
def test_generate_figure3():
x=np.arange(1,101)
y1=x
y2=x**2
y3=x**3
fig=plt.figure(figsize=(8,6))
g=gridspec.GridSpec(2,2)
axes1=plt.subplot(g[0,0],title='y = x')
axes1.plot(x,y1)
axes2=plt.subplot(g[1,0],title='y = x**2')
axes2.plot(x,y2)
m
er as
axes3=plt.subplot(g[:,1], title='y = x**3')
co
axes3.plot(x,y3)
eH w
plt.tight_layout()
o.
plt.show() rs e
plt.savefig('testfigure3.png')
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 CourseHero.com on 12-10-2021 00:08:43 GMT -06:00
https://www.coursehero.com/file/85760630/Hands-on-Data-Visualization-using-matplotlibtxt/
Powered by TCPDF (www.tcpdf.org)