Matplotlib 可能是 Python 2D-绘图领域使用最广泛的套件。它能让使用者很轻松地将数据图形化,并且提供多样化的输出格式。
from pylab import *

size = 128,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,0,1,1], frameon=False) rcParams['text.antialiased'] = False
text(0.5,0.5,"Aliased",ha='center',va='center') plt.xlim(0,1),plt.ylim(0,1),
plt.xticks([]),plt.yticks([])

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

size = 256,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,0.1,1,.8], frameon=False) for i in range(1,11):
plt.axvline(i, linewidth=1, color='blue',alpha=.25+.75*i/10.) xlim(0,11)
xticks([]),yticks([])

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

size = 128,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,0,1,1], frameon=False) rcParams['text.antialiased'] = True
text(0.5,0.5,"Anti-aliased",ha='center',va='center') plt.xlim(0,1),plt.ylim(0,1),
plt.xticks([]),plt.yticks([])
from pylab import *

axes([0.1,0.1,.8,.8])
xticks([]), yticks([])
text(0.6,0.6, 'axes([0.1,0.1,.8,.8])',ha='center',va='center',size=20,alpha=.5) axes([0.2,0.2,.3,.3])
xticks([]), yticks([])
text(0.5,0.5, 'axes([0.2,0.2,.3,.3])',ha='center',va='center',size=16,alpha=.5)

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

axes([0.1,0.1,.5,.5])
xticks([]), yticks([])
text(0.1,0.1, 'axes([0.1,0.1,.5,.5])',ha='left',va='center',size=16,alpha=.5) axes([0.2,0.2,.5,.5])
xticks([]), yticks([])
text(0.1,0.1, 'axes([0.2,0.2,.5,.5])',ha='left',va='center',size=16,alpha=.5) axes([0.3,0.3,.5,.5])
xticks([]), yticks([])
text(0.1,0.1, 'axes([0.3,0.3,.5,.5])',ha='left',va='center',size=16,alpha=.5) axes([0.4,0.4,.5,.5])
xticks([]), yticks([])
text(0.1,0.1, 'axes([0.4,0.4,.5,.5])',ha='left',va='center',size=16,alpha=.5) # plt.savefig("../figures/axes-2.png",dpi=64)
show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt fig = plt.figure(figsize=(5,4),dpi=72)
axes = fig.add_axes([0.01, 0.01, .98, 0.98])
X = np.linspace(0,2,200,endpoint=True)
Y = np.sin(2*np.pi*X)
plt.plot (X, Y, lw=.25, c='k')
plt.xticks(np.arange(0.0, 2.0, 0.1))
plt.yticks(np.arange(-1.0,1.0, 0.1))
plt.grid()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt n = 12
X = np.arange(n)
Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) plt.axes([0.025,0.025,0.95,0.95])
plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white') for x,y in zip(X,Y1):
plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom') for x,y in zip(X,Y2):
plt.text(x+0.4, -y-0.05, '%.2f' % y, ha='center', va= 'top') plt.xlim(-.5,n), plt.xticks([])
plt.ylim(-1.25,+1.25), plt.yticks([]) # savefig('../figures/bar_ex.png', dpi=48)
plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt fig = plt.figure(figsize=(8,5),dpi=72)
fig.patch.set_alpha(0.0)
axes = plt.subplot(111) n = 5
Z = np.zeros((n,4))
X = np.linspace(0,2,n,endpoint=True)
Y = np.random.random((n,4))
plt.boxplot(Y) #plt.xlim(-0.2,4.2)
#plt.ylim(-1.2,1.2)
plt.xticks([]), plt.yticks([]) plt.text(-0.05, 1.05, " Box Plot \n\n",
horizontalalignment='left',
verticalalignment='top',
family='Lint McCree Intl BB',
size='x-large',
bbox=dict(alpha=1.0, width=350,height=60),
transform = axes.transAxes) plt.text(-0.05, .95, " Make a box and whisker plot ",
horizontalalignment='left',
verticalalignment='top',
family='Lint McCree Intl BB',
size='medium',
transform = axes.transAxes) plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

size = 256,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,0.1,1,.8], frameon=False) for i in range(1,11):
plot( [i,i], [0,1], lw=1.5 )
xlim(0,11)
xticks([]),yticks([])

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

def colormap(cmap,filename):
n = 512
Z = np.linspace(0,1,n,endpoint=True).reshape((1,n))
size = 512,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0.,0.,1.,1.], frameon=False)
xticks([]), yticks([])
imshow(Z,aspect='auto',cmap=cmap,origin="lower") cmaps = [m for m in cm.datad if not m.endswith("_r")]
cmaps.sort() for i in range(len(cmaps)):
name = cmaps[i]
filename = name
if name == 'Spectral':
filename = 'spectral-2'
colormap(name,filename)

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)

n = 256
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
X,Y = np.meshgrid(x,y) contourf(X, Y, f(X,Y), 8, alpha=.75, cmap=cm.hot)
C = contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5)
clabel(C, inline=1, fontsize=10)
xticks([]), yticks([]) text(-0.05, 1.05, " Contour Plot \n\n",
horizontalalignment='left',
verticalalignment='top',
family='Lint McCree Intl BB',
size='x-large',
bbox=dict(facecolor='white', alpha=1.0, width=350,height=60),
transform = gca().transAxes) text(-0.05, .975, " Draw contour lines and filled contours ",
horizontalalignment='left',
verticalalignment='top',
family='Lint McCree Intl BB',
size='medium',
transform = gca().transAxes)

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt def f(x,y):
return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) n = 256
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
X,Y = np.meshgrid(x,y) plt.axes([0.025,0.025,0.95,0.95]) plt.contourf(X, Y, f(X,Y), 8, alpha=.75, cmap=plt.cm.hot)
C = plt.contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5)
plt.clabel(C, inline=1, fontsize=10) plt.xticks([]), plt.yticks([])
# savefig('../figures/contour_ex.png',dpi=48)
plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

size = 256,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,0,1,1], frameon=False) plot(np.arange(4), np.ones(4), color="blue", dashes=[15,15], linewidth=8, dash_capstyle = 'butt') plot(5+np.arange(4), np.ones(4), color="blue", dashes=[15,15], linewidth=8, dash_capstyle = 'round') plot(10+np.arange(4), np.ones(4), color="blue", dashes=[15,15], linewidth=8, dash_capstyle = 'projecting') xlim(0,14)
xticks([]),yticks([])
show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

size = 256,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,0,1,1], frameon=False) plot(np.arange(3), [0,1,0], color="blue", dashes=[12,5], linewidth=8, dash_joinstyle = 'miter')
plot(4+np.arange(3), [0,1,0], color="blue", dashes=[12,5], linewidth=8, dash_joinstyle = 'bevel')
plot(8+np.arange(3), [0,1,0], color="blue", dashes=[12,5], linewidth=8, dash_joinstyle = 'round') xlim(0,12), ylim(-1,2)
xticks([]),yticks([]) show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C,S = np.cos(X), np.sin(X)
plt.plot(X,C)
plt.plot(X,S) plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

# Imports
import numpy as np
import matplotlib.pyplot as plt # Create a new figure of size 8x6 points, using 100 dots per inch
plt.figure(figsize=(8,6), dpi=100) # Create a new subplot from a grid of 1x1
plt.subplot(111) X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X) # Plot cosine using blue color with a continuous line of width 1 (pixels)
plt.plot(X, C, color="blue", linewidth=1.0, linestyle="-") # Plot sine using green color with a continuous line of width 1 (pixels)
plt.plot(X, S, color="green", linewidth=1.0, linestyle="-") # Set x limits
plt.xlim(-4.0,4.0) # Set x ticks
plt.xticks(np.linspace(-4,4,9,endpoint=True)) # Set y limits
plt.ylim(-1.0,1.0) # Set y ticks
plt.yticks(np.linspace(-1,1,5,endpoint=True)) # Save figure using 72 dots per inch
# savefig("../figures/exercice_2.png",dpi=72) # Show result on screen
plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt plt.figure(figsize=(8,5), dpi=80)
plt.subplot(111) X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X) plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-") plt.xlim(-4.0,4.0)
plt.xticks(np.linspace(-4,4,9,endpoint=True)) plt.ylim(-1.0,1.0)
plt.yticks(np.linspace(-1,1,5,endpoint=True)) plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt plt.figure(figsize=(8,5), dpi=80)
plt.subplot(111) X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X) plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-") plt.xlim(X.min()*1.1, X.max()*1.1)
plt.ylim(C.min()*1.1,C.max()*1.1) plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

figure(figsize=(8,5), dpi=80)
subplot(111) X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X) plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plot(X+.1, C, color="blue", linewidth=2.5, linestyle="-",alpha=.15)
plot(X, S, color="red", linewidth=2.5, linestyle="-") xlim(X.min()*1.1, X.max()*1.1)
ylim(C.min()*1.1,C.max()*1.1) # savefig("../figures/exercice_4.png",dpi=72)
show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt plt.figure(figsize=(8,5), dpi=80)
plt.subplot(111) X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X) plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-") plt.xlim(X.min()*1.1, X.max()*1.1)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi]) plt.ylim(C.min()*1.1,C.max()*1.1)
plt.yticks([-1, 0, +1]) plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt plt.figure(figsize=(8,5), dpi=80)
plt.subplot(111) X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X) plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-") plt.xlim(X.min()*1.1, X.max()*1.1)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']) plt.ylim(C.min()*1.1,C.max()*1.1)
plt.yticks([-1, 0, +1],
[r'$-1$', r'$0$', r'$+1$']) plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt plt.figure(figsize=(8,5), dpi=80)
ax = plt.subplot(111) ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0)) X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X) plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-") plt.xlim(X.min()*1.1, X.max()*1.1)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']) plt.ylim(C.min()*1.1,C.max()*1.1)
plt.yticks([-1, 0, +1],
[r'$-1$', r'$0$', r'$+1$']) plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt plt.figure(figsize=(8,5), dpi=80)
ax = plt.subplot(111)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0)) X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X) plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine") plt.xlim(X.min()*1.1, X.max()*1.1)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']) plt.ylim(C.min()*1.1,C.max()*1.1)
plt.yticks([-1, +1],
[r'$-1$', r'$+1$']) plt.legend(loc='upper left', frameon=False)
# plt.savefig("../figures/exercice_8.png",dpi=72)
plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt plt.figure(figsize=(8,5), dpi=80)
ax = plt.subplot(111)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0)) X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X) plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine") plt.xlim(X.min()*1.1, X.max()*1.1)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']) plt.ylim(C.min()*1.1,C.max()*1.1)
plt.yticks([-1, +1],
[r'$-1$', r'$+1$']) t = 2*np.pi/3
plt.plot([t,t],[0,np.cos(t)],
color ='blue', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.cos(t),], 50, color ='blue')
plt.annotate(r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
xy=(t, np.cos(t)), xycoords='data',
xytext=(-90, -50), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")) plt.plot([t,t],[0,np.sin(t)],
color ='red', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.sin(t),], 50, color ='red')
plt.annotate(r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
xy=(t, np.sin(t)), xycoords='data',
xytext=(+10, +30), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")) plt.legend(loc='upper left', frameon=False)
#plt.savefig("../figures/exercice_9.png",dpi=72)
plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt plt.figure(figsize=(8,5), dpi=80)
ax = plt.subplot(111)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0)) X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X) plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine",
zorder=-1)
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine",
zorder=-2) plt.xlim(X.min()*1.1, X.max()*1.1)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']) plt.ylim(C.min()*1.1,C.max()*1.1)
plt.yticks([-1, +1],
[r'$-1$', r'$+1$']) plt.legend(loc='upper left', frameon=False) t = 2*np.pi/3
plt.plot([t,t],[0,np.cos(t)],
color ='blue', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.cos(t),], 50, color ='blue')
plt.annotate(r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
xy=(t, np.sin(t)), xycoords='data',
xytext=(+10, +30), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")) plt.plot([t,t],[0,np.sin(t)],
color ='red', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.sin(t),], 50, color ='red')
plt.annotate(r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
xy=(t, np.cos(t)), xycoords='data',
xytext=(-90, -50), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")) for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(16)
label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65 )) #plt.savefig("../figures/exercice_10.png",dpi=72)
plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt fig = plt.figure(figsize=(5,4),dpi=72)
axes = fig.add_axes([0.01, 0.01, .98, 0.98]) #, frameon=False)
X = np.linspace(0,2,200,endpoint=True)
Y = np.sin(2*np.pi*X)
plt.plot (X, Y, lw=2)
plt.ylim(-1.1,1.1)
plt.grid()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt ax = plt.axes([0.025,0.025,0.95,0.95]) ax.set_xlim(0,4)
ax.set_ylim(0,3)
ax.xaxis.set_major_locator(plt.MultipleLocator(1.0))
ax.xaxis.set_minor_locator(plt.MultipleLocator(0.1))
ax.yaxis.set_major_locator(plt.MultipleLocator(1.0))
ax.yaxis.set_minor_locator(plt.MultipleLocator(0.1))
ax.grid(which='major', axis='x', linewidth=0.75, linestyle='-', color='0.75')
ax.grid(which='minor', axis='x', linewidth=0.25, linestyle='-', color='0.75')
ax.grid(which='major', axis='y', linewidth=0.75, linestyle='-', color='0.75')
ax.grid(which='minor', axis='y', linewidth=0.25, linestyle='-', color='0.75')
ax.set_xticklabels([])
ax.set_yticklabels([]) # savefig('../figures/grid_ex.png',dpi=48)
plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *
import matplotlib.gridspec as gridspec G = gridspec.GridSpec(3, 3) axes_1 = subplot(G[0, :])
xticks([]), yticks([])
text(0.5,0.5, 'Axes 1',ha='center',va='center',size=24,alpha=.5) axes_2 = subplot(G[1,:-1])
xticks([]), yticks([])
text(0.5,0.5, 'Axes 2',ha='center',va='center',size=24,alpha=.5) axes_3 = subplot(G[1:, -1])
xticks([]), yticks([])
text(0.5,0.5, 'Axes 3',ha='center',va='center',size=24,alpha=.5) axes_4 = subplot(G[-1,0])
xticks([]), yticks([])
text(0.5,0.5, 'Axes 4',ha='center',va='center',size=24,alpha=.5) axes_5 = subplot(G[-1,-2])
xticks([]), yticks([])
text(0.5,0.5, 'Axes 5',ha='center',va='center',size=24,alpha=.5) #plt.savefig('../figures/gridspec.png', dpi=64)
show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

import numpy as np
import matplotlib.pyplot as plt def f(x,y):
return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) n = 10
x = np.linspace(-3,3,3.5*n)
y = np.linspace(-3,3,3.0*n)
X,Y = np.meshgrid(x,y)
Z = f(X,Y) plt.axes([0.025,0.025,0.95,0.95])
plt.imshow(Z,interpolation='bicubic', cmap='bone', origin='lower')
plt.colorbar(shrink=.92) plt.xticks([]), plt.yticks([])
# savefig('../figures/imshow_ex.png', dpi=48)
plt.show()

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

def linestyle(ls,name):
size = 256,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,0,1,1],frameon=False)
X = np.arange(11)
Y = np.ones(11)
plot(X,Y,ls,color=(.0,.0,1,1), lw=3, ms=10, mfc=(.75,.75,1,1), mec=(0,0,1,1))
xlim(0,10)
xticks([]), yticks([]) for ls in ['-','--',':',',','o','^','v','<','>','s',
'+','x','d','','','','','h','p','|','_']:
linestyle(ls,ls)
linestyle('D', 'dd')
linestyle('H', 'hh')
linestyle('.', 'dot')
linestyle('-.', '-dot')

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

size = 256,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,.1,1,.8], frameon=False) for i in range(1,11):
plot( [i,i], [0,1], color='b', lw=i/2. ) xlim(0,11),ylim(0,1)
xticks([]),yticks([])

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

def marker(m,name):
size = 256,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,0,1,1],frameon=False)
X = np.arange(11)
Y = np.ones(11)
plot(X,Y,color='w', lw=1, marker=m, ms=10, mfc=(.75,.75,1,1), mec=(0,0,1,1))
xlim(0,10)
xticks([]), yticks([]) for m in [0,1,2,3,4,5,6,7,'o','h','_','','','','','','p',
'^','v','<','>','|','d',',','+','s','*','|','x']:
if type(m) is int:
marker(m, 'i%d' % m)
else:
marker(m,m) marker('D', 'dd')
marker('H', 'hh')
marker('.', 'dot')
marker(r"$\sqrt{2}$", "latex")

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

size = 256,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,0,1,1], frameon=False) for i in range(1,11):
r,g,b = np.random.uniform(0,1,3)
plot([i,],[1,],'s', markersize=5, markerfacecolor='w',
markeredgewidth=1.5, markeredgecolor=(r,g,b,1))
xlim(0,11)
xticks([]),yticks([])

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

size = 256,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,0,1,1], frameon=False) for i in range(1,11):
plot([i,],[1,],'s', markersize=5,
markeredgewidth=1+i/10., markeredgecolor='k', markerfacecolor='w')
xlim(0,11)
xticks([]),yticks([])

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

size = 256,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,0,1,1], frameon=False) for i in range(1,11):
r,g,b = np.random.uniform(0,1,3)
plot([i,],[1,],'s', markersize=8, markerfacecolor=(r,g,b,1),
markeredgewidth=.1, markeredgecolor=(0,0,0,.5))
xlim(0,11)
xticks([]),yticks([])

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

from pylab import *

size = 256,16
dpi = 72.0
figsize= size[0]/float(dpi),size[1]/float(dpi)
fig = figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
axes([0,0,1,1], frameon=False) for i in range(1,11):
plot([i,],[1,],'s', markersize=i, markerfacecolor='w',
markeredgewidth=.5, markeredgecolor='k')
xlim(0,11)
xticks([]),yticks([])

吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(1)-LMLPHP

05-01 03:28