1 Prepare the Data
import numpy as np
x=np.linspace(0,10,100)
y=np.cos(x)
z=np.sin(x)
Data or Images
- narray
- mgrid
- images
data = 2 * np.random.random((10, 10))
data2 = 3 * np.random.random((10, 10))
Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
from matplotlib.cbook import get_sample_data
img = np.load(get_sample_data('axes_grid/bivariate_normal.npy'))
2 Creat Plot
- creat figure object
- creat subplot object
import matplotlib.pyplot as plt
fig = plt.figure()
fig2 = plt.figure(figsize=plt.figaspect(2.0))
fig.add_axes()
ax1 = fig.add_subplot(221)
ax3 = fig.add_subplot(212)
fig3, axes = plt.subplots(nrows=2,ncols=2)
fig4, axes2 = plt.subplots(ncols=3)
3 Plotting Routines
- plot with line
- draw unconnected point
lines = ax.plot(x,y)
ax.scatter(x,y)
axes[0,0].bar([1,2,3],[3,4,5])
axes[1,0].barh([0.5,1,2.5],[0,1,2])
axes[1,1].axhline(0.45)
axes[0,1].axvline(0.65)
ax.fill(x,y,color='blue')
ax.fill_between(x,y,color='yellow')
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax3 = fig.add_subplot(223)
lines = ax1.plot(x,y)
ax3.scatter(x,y)
ax3.set_xlim(0,10)
ax2 = fig.add_subplot(222)
ax4 = fig.add_subplot(224)
ax2.fill(x,y,color='blue')
ax4.fill_between(x,y,color='yellow')
fig
axes[0,0].bar([1,2,3],[3,4,5])
axes[1,0].barh([0.5,1,2.5],[0,1,2])
axes[1,1].axhline(0.45)
axes[0,1].axvline(0.65)
fig3
Vector Fields
- object : subplot
- method?func : arrow
- attribution :
fig4, axes2 = plt.subplots(nrows=2,ncols=1)
axes2[0].arrow(0,0,0.5,0.5)
axes2[1].quiver(y,z)
fig4
?plt.subplots().arrow
fig5, axes3=plt.subplots()
Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
axes3.streamplot(X,Y,U,V)
fig5
Data Distributions
- Plot a histogram
- Make a box and whisker plot
- Make a violin plot
from numpy.random import randn
fig6, axes4=plt.subplots(nrows=3,ncols=1)
datay=randn(100)
axes4[0].hist(datay,bins=20,color='k',alpha=0.3)
axes4[1].boxplot(2*datay+1)
axes4[2].violinplot(datay)
fig6