matplotlib_cheat_sheet

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

  1. narray
  2. mgrid
  3. 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) # row-col-num
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)# Draw points with lines or markers connecting them
ax.scatter(x,y) #Draw unconnected points, scaled or colored
axes[0,0].bar([1,2,3],[3,4,5])# Plot vertical rectangles (constant width)
axes[1,0].barh([0.5,1,2.5],[0,1,2])# Plot horiontal rectangles (constant height)
axes[1,1].axhline(0.45) #Draw a horizontal line across axes
axes[0,1].axvline(0.65) #Draw a vertical line across axes
ax.fill(x,y,color='blue') #Draw filled polygons
ax.fill_between(x,y,color='yellow') #Fill between y-values and 0
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)    #set axis limits
#plt.clf() clear figure
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]) # Plot vertical rectangles (constant width)
axes[1,0].barh([0.5,1,2.5],[0,1,2])# Plot horiontal rectangles (constant height)
axes[1,1].axhline(0.45) #Draw a horizontal line across axes
axes[0,1].axvline(0.65) #Draw a vertical line across axes
fig3

这里写图片描述

Vector Fields

  • object : subplot
  • method?func : arrow
  • attribution :
fig4, axes2 = plt.subplots(nrows=2,ncols=1)    #axes is a array.
axes2[0].arrow(0,0,0.5,0.5)
axes2[1].quiver(y,z)
fig4

这里写图片描述

?plt.subplots().arrow
fig5, axes3=plt.subplots()    # default : nrows=1,ncols=1
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
    • e.g. X~N(1,4)
  • 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) #Plot a histogram
axes4[1].boxplot(2*datay+1) #Make a box and whisker plot
axes4[2].violinplot(datay)
fig6

这里写图片描述

posted @ 2017-06-22 22:43  codie  阅读(334)  评论(0编辑  收藏  举报