Data Visualization with maplotlib
My First Plot
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()
t = [5, 10, 15, 20, 25]
d = [25, 50, 75, 100, 125]
[Link](title='Time vs Distance Covered',xlabel='time (seconds)',ylabel= "distance
(meters)",xlim=(0,30),ylim=(0,130))
[Link](t,d,label='d=5t')
[Link]()
[Link]('[Link]')
test_my_first_plot()
Create Line and Scatter Plots
import matplotlib
[Link]('Agg')
import [Link] as plt
import numpy as np
import [Link] as gridspec
from [Link] import image_comparison
#Write your code here
def test_sine_wave_plot():
fig = [Link](figsize =(12,3))
ax= fig.add_subplot()
t = [Link](0.0,2.0,220)
v = [Link](2.5*[Link]*t)
[Link](t,v, color= 'red', label = 'sin(t)')
[Link](xlabel = 'Time(seconds)', ylabel ='Voltage (mV)', title = 'Sine Wave', xlim= (0,2), ylim =(-1,1))
ax.set_xticks([0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0])
ax.set_yticks([-1,0,1])
[Link](linestyle='--')
[Link]()
[Link]('[Link]')
test_sine_wave_plot()
#Task2
def test_multi_curve_plot():
fig = [Link](figsize=(12,3))
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',marker = 'o',color = 'red')
[Link](x, y2, label='y = x**2',marker = "s",color = 'green')
[Link](x, y3, label='y = x**3',marker = "v",color = 'blue')
[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]
months = [1,2,3,4,5,6,7,8,9,10,11,12]
[Link](title="Cars Sold by Company 'X' in 2017",xlabel='Months', ylabel='No. of Cars Sold',xlim=(0,
13), ylim=(20,100))
[Link](months, s,marker = 'o',color = 'red')
[Link]([1, 3, 5, 7, 9,11])
ax.set_xticklabels(['Jan', 'Mar', 'May', 'Jul', 'Sep','Nov'])
[Link]('[Link]')
test_scatter_plot()
BAR PLOTS
import matplotlib
[Link]('Agg')
import [Link] as plt
import numpy as np
import [Link] as gridspec
#Write your code here
def test_barplot_of_iris_sepal_length():
fig = [Link](figsize = (8,6))
ax = fig.add_subplot(111)
species = ['setosa', 'versicolor', 'viriginica']
index = [0.2, 1.2, 2.2]
sepal_len = [5.01, 5.94, 6.59]
[Link](title = "Mean Sepal Length of Iris Species",xlabel = "Specices",ylabel = "Sepal Length
(cm)",xlim = (0,3),ylim = (0,7))
[Link](index,sepal_len,width = 0.5,color = "red",edgecolor = "black")
[Link]([0.45,1.45,2.45])
ax.set_xticklabels(['setosa', 'versicolor', 'viriginica'])
[Link]()
[Link]('bar_iris_sepal.png')
test_barplot_of_iris_sepal_length()
def test_barplot_of_iris_sepal_length():
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](title = "Mean Measurements of Iris Species",xlabel = "Specices",ylabel = "Iris Measurements
(cm)",xlim = (0.5,3.7),ylim = (0,10))
[Link](species_index1, sepal_len, color='c', width=0.2, edgecolor='black', label='Sepal Length')
[Link](species_index2, sepal_wd, color='m', width=0.2, edgecolor='black', label='Sepal Width')
[Link](species_index3, petal_len, color='y', width=0.2, edgecolor='black', label='Petal Length')
[Link](species_index4, petal_wd, color='orange', width=0.2, edgecolor='black', label='Petal
Width')
ax.set_xticks([1.1,2.1,3.1])
ax.set_xticklabels(['setosa', 'versicolor','viriginica'])
[Link]()
[Link]('bar_iris_measure.png')
test_barplot_of_iris_sepal_length()
def test_hbarplot_of_iris_petal_length():
fig = [Link](figsize =(12,5))
ax = fig.add_subplot(111)
species = ['setosa', 'versicolor', 'viriginica']
index = [0.2, 1.2, 2.2]
petal_len = [1.46, 4.26, 5.55]
[Link](title = "Mean Petal Length of Iris Species",ylabel = "Specices",xlabel = "Petal Length (cm)")
[Link](index, petal_len, color='c', height = 0.5, edgecolor='black', label='Sepal Length')
ax.set_yticks([0.45, 1.45,2.45])
ax.set_yticklabels(['setosa', 'versicolor','viriginica'])
[Link]('bar_iris_petal.png')
test_hbarplot_of_iris_petal_length()
Histograms and Boxplot
import matplotlib
[Link]('Agg')
import [Link] as plt
import numpy as np
import [Link] as gridspec
#Write your code here
def test_hist_of_a_sample_normal_distribution():
fig = [Link](figsize = (8,6))
ax = fig.add_subplot(111)
[Link](100)
x1 = 25+3*[Link](1000)
[Link](title = "Histogram of a Single Dataset",xlabel = "x1",ylabel = "Bin Count")
[Link](x1,bins = 30)
[Link]('histogram_normal.png')
test_hist_of_a_sample_normal_distribution()
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)
[Link](title = "Box plot of Multiple Datasets",xlabel = "Dataset",ylabel = "Value")
[Link]([x1, x2, x3, x4], labels=['X1', 'X2', 'X3', 'X4'], notch=True,patch_artist = "True",showfliers
= "+")
[Link]('box_distribution.png')
test_boxplot_of_four_normal_distribution()
Apply Matplotlib Styles
import matplotlib
[Link]('Agg')
import [Link] as plt
import numpy as np
import [Link] as gridspec
#Write your code here
def test_generate_plot_with_style1():
with [Link]('ggplot'):
fig = [Link](figsize=(8,6))
ax = fig.add_subplot()
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](title = "Mean Measurements of Iris Species",xlabel = "Specices",ylabel = "Iris
Measurements (cm)",xlim=(0.5,3.7),ylim = (0,10))
[Link](species_index1, sepal_len, color='c', width=0.2, edgecolor='black', label='Sepal Length')
[Link](species_index2, sepal_wd, color='m', width=0.2, edgecolor='black', label='Sepal Width')
[Link](species_index3, petal_len, color='y', width=0.2, edgecolor='black', label='Petal Length')
[Link](species_index4, petal_wd, color='orange', width=0.2, edgecolor='black', label='Petal
Width')
ax.set_xticks([1.1,2.1,3.1])
ax.set_xticklabels(['setosa', 'versicolor','viriginica'])
[Link]()
[Link]('[Link]')
test_generate_plot_with_style1()
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]
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,width=0.2,color='c',edgecolor='black',label='Sepal Length')
[Link](species_index2, sepal_wd,width=0.2,color='m',edgecolor='black',label='Sepal Width')
[Link](species_index3, petal_len,width=0.2,color='y',edgecolor='black',label='Petal Length')
[Link](species_index4, petal_wd,width=0.2,color='orange',edgecolor='black',label='Petal
Width')
species=['setosa', 'versicolor', 'viriginica']
index= [0.2, 1.2, 2.2]
sepal_len=[5.01, 5.94, 6.59]
[Link](title='Mean Measurements of Iris Species',
xlabel='Species', ylabel='Iris Measurements (cm)',xlim=(0.5, 3.7), ylim=(0,10))
quarters = [1.1, 2.1, 3.1]
[Link](index, sepal_len,width=0.5,color='red',edgecolor='black')
ax.set_xticks(quarters)
ax.set_xticklabels(['setosa', 'versicolor', 'viriginica'])
[Link]()
[Link]('[Link]')
test_generate_plot_with_style2()
# Write your functionality below
def test_generate_plot_with_style3():
with [Link]([ 'grayscale']):
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,width=0.2,color='c',edgecolor='black',label='Sepal Length')
[Link](species_index2, sepal_wd,width=0.2,color='m',edgecolor='black',label='Sepal Width')
[Link](species_index3, petal_len,width=0.2,color='y',edgecolor='black',label='Petal Length')
[Link](species_index4, petal_wd,width=0.2,color='orange',edgecolor='black',label='Petal
Width')
species=['setosa', 'versicolor', 'viriginica']
index= [0.2, 1.2, 2.2]
sepal_len=[5.01, 5.94, 6.59]
[Link](title='Mean Measurements of Iris Species',
xlabel='Species', ylabel='Iris Measurements (cm)',xlim=(0.5, 3.7), ylim=(0,10))
quarters = [1.1, 2.1, 3.1]
[Link](index, sepal_len,width=0.5,color='red',edgecolor='black')
ax.set_xticks(quarters)
ax.set_xticklabels(['setosa', 'versicolor', 'viriginica'])
[Link]()
[Link]('[Link]')
test_generate_plot_with_style3()