Matplotlib Hands On
Matplotlib Hands On
fig = plt.figure(figsize=(8,6))
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]
ax = fig.add_subplot(111)
plt.bar(species_index1,sepal_len,color = 'c',edgecolor = 'black',width = 0.2,label = 'Sepal lenght')
plt.bar(species_index2,sepal_wd,color = 'm',edgecolor = 'black',width = 0.2,label = 'Sepal
Width')
plt.bar(species_index3,petal_len,color = 'y',edgecolor = 'black',width = 0.2,label = 'Petal length')
plt.bar(species_index4,petal_wd,color = 'orange',edgecolor = 'black',width = 0.2,label = 'Petal
Width')
ax.set(xlabel = 'Species',ylabel = 'Iris Measurements (cm)',xlim = (0.5,3.7),ylim = (0,10))
ticks = [1.1,2.1,3.1]
labs = ['setosa','versicolor','viriginica']
plt.xticks(ticks,labs)
plt.legend()
fig = plt.figure(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]
plt.barh(index,petal_len,height = 0.5,color = 'c',edgecolor = 'black')
ax.set(xlabel = 'Species',ylabel = 'Petal Length (cm)',title = 'Mean Petal Length of Iris Species')
ticks = [0.45,1.45,2.45]
labs = ['setosa','versicolor','viriginica']
plt.yticks(ticks,labs)
HISTOGRAMS
Box Plot
fig = plt.figure(figsize = (8,6))
ax = fig.add_subplot(111)
np.random.seed(100)
x1 = np.random.randn(1000) * 3 + 25
x2 = np.random.randn(1000) * 5 + 35
x3 = np.random.randn(1000) * 10 + 55
x4 = np.random.randn(1000) * 3 + 45
labels = ['X1','X2','X3','X4']
ax.set(xlabel = 'Dataset',ylabel = 'Value',title = 'Box plot of Multiple Datasets',label = labels)
plt.boxplot(x1,notch = True,patch_artist = True,sym = '+')
plt.boxplot(x2,notch = True,patch_artist = True,sym = '+')
plt.boxplot(x3,notch = True,patch_artist = True,sym = '+')
plt.boxplot(x4,notch = True,patch_artist = True,sym = '+')
Matplotlib Styles
with plt.style.context('grayscale'):
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]
plt.bar(species_index1,sepal_len,width = 0.2,label = 'Sepal Length')
plt.bar(species_index2,sepal_wd,width = 0.2,label = 'Sepal Width')
plt.bar(species_index3,petal_len,width = 0.2,label = 'Petal Length')
plt.bar(species_index4,petal_wd,width = 0.2,label = 'Petal Width')
ax.set(xlabel = 'Species',ylabel = 'Iris Measurements (cm)',xlim = (0.5,3.7),ylim = (0,10),title =
'Mean Measurements of Iris Species')
tcks = (1.1,2.1,3.1)
labs = ('setosa','versicolor','viriginica')
plt.xticks(tcks,labs)
plt.legend()
t = np.arange(0.0,5.0,0.01)
s1 = np.sin(2 * np.pi * t)
s2 = np.sin(4 * np.pi * t)
fig = plt.figure(figsize = (8,6))
axes1 = plt.subplot(211,title = 'Sin(2pix)')
axes1.plot(t,s1)
axes2 = plt.subplot(212,title = 'Sin(4pix)',sharex = axes1)
axes2.sharey = axes2
axes2.plot(t,s2)
np.random.seed(1000)
x = np.random.rand(10)
y = np.random.rand(10)
z = np.sqrt(x**2 + y**2)
fig = plt.figure(figsize = (8,6))
axes1 = plt.subplot(221,title = 'Scatter plot with Upper Traingle Markers')
axes1.scatter(x,y,s = 80,c = z,marker = '^')
xtcks1 = (0.0,0.4,0.8,1.2)
ytcks1 = (-0.2,0.2,0.6,1.0)
axes1.set_xticks(xtcks1)
axes1.set_yticks(ytcks1)
axes2 = plt.subplot(222,title = 'Scatter plot with Plus Markers')
axes2.scatter(x,y,s = 80,c = z,marker = '+')
xtcks2 = (0.0, 0.4, 0.8, 1.2)
ytcks2 = (-0.2, 0.2, 0.6, 1.0)
axes2.set(xticks = xtcks2,yticks = ytcks2)
axes3 = plt.subplot(223,title = 'Scatter plot with Circle Markers')
axes3.scatter(x,y,s = 80,c = z,marker = 'o')
xtcks3 = (0.0, 0.4, 0.8, 1.2)
ytcks3 = (-0.2, 0.2, 0.6, 1.0)
axes3.set(xticks = xtcks3,yticks = ytcks3)
axes4 = plt.subplot(224,title = 'Scatter plot with Diamond Markers')
axes4.scatter(x,y,s = 80,c = z,marker = 'd')
xtcks4 = (0.0, 0.4, 0.8, 1.2)
ytcks4 = (-0.2, 0.2, 0.6, 1.0)
axes4.set(xticks = xtcks3,yticks = ytcks3)
plt.tight_layout()
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)
axes3 = plt.subplot(g[0:,1],title = 'y = x**3')
axes3.plot(x,y3)
plt.tight_layout()