# Axis setting# label
plt.xlabel("Im x")
plt.ylabel("Im y")# limit
plt.xlim((-3,3))
plt.ylim((-5,5))# ticks
plt.xticks(np.linspace(-3,3,9))
plt.yticks([-5,-2,0,2,5],['very low','low','normal','high','very high'])# gca(): get current axis
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')# coordinate display position
ax.xaxis.set_ticks_position('top')
ax.yaxis.set_ticks_position('right')# set axis position
ax.spines['bottom'].set_position(('data',0))#data axes
ax.spines['left'].set_position(('data',0))# tick labelsfor label in ax.get_xticklabels()+ ax.get_yticklabels():
label.set_fontsize(12)
label.set_bbox(dict(facecolor='red',edgecolor='None',alpha=0.7))
4.注释和文本标注
# Annotation
x = np.linspace(-3,3,30)
y = x**2
x0 =2
y0 = x0**2
plt.plot(x,y)# annotate
plt.annotate('x^2=%d'%y0,xy=(x0,y0),xycoords='data',xytext=(+30,-30),textcoords='offset points',
fontsize=12,arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.1'))# text
fontdict ={'family':'Times New Roman','weight':'normal','size':15,'color':'blue'}
plt.text(-3,3,'This is a text annotition',fontdict=fontdict)
plt.show()
5.散点图
# Scatter
X = np.random.normal(0,1,1024)
Y = np.random.normal(0,1,1024)
colors= np.arctan2(Y,X)
plt.scatter(X,Y,s=75,c=colors,alpha=0.5)
plt.show()
6.条形图
# Bar
X = np.arange(12)
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.bar(X,+Y1,facecolor='blue',edgecolor='white')
plt.bar(X,-Y2,facecolor='grey',edgecolor='white')# labelfor x,y inzip(X,Y1):
plt.text(x,y+0.05,'%.2f'%y,ha='center',va='bottom')for x,y inzip(X,Y2):
plt.text(x,-y-0.05,'-%.2f'%y,ha='center',va='top')
plt.show()
7.等高线图
# Contours# backstepping functiondeff(x,y):return x**2+ y**2# meshgrid
n=256
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
X,Y = np.meshgrid(x,y)# use plt.contourf to filling contours
plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.coolwarm)#cool# use plt.contour to add contour lines
C = plt.contour(X,Y,f(X,Y),10,colors='black',linewidth=0.5)# adding label
plt.clabel(C,inline=True,fontsize=10)
plt.show()
8.3D
from mpl_toolkits.mplot3d import Axes3D
#3D
fig = plt.figure(0)
ax = Axes3D(fig)
X = np.linspace(-4,4,20)
Y = np.linspace(-4,4,20)
X,Y = np.meshgrid(X,Y)
R = np.sqrt(X**2+Y**2)
Z = np.sin(R)
ax.set_xlim(-4,4)
ax.set_ylim(-4,4)
ax.set_zlim(-2,2)# surface
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))# contourf
ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap='rainbow')
plt.show()