散点图
import matplotlib.pyplot as plt
import numpy as np
n = 1024
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)
T = np.arctan2(Y,X) ## 反正切, for color value
plt.scatter(X,Y,s=75,c=T,alpha=0.5) ## c:color s:size,alpha:透明度
plt.xlim((-1.5,1.5))
plt.ylim((-1.5,1.5))
plt.xticks(()) ## 隐藏x刻度
plt.yticks(()) ## 隐藏y刻度
plt.show()
漂亮的小点点
柱状图
import matplotlib.pyplot as plt
import numpy as np
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.bar(X,+Y1,facecolor='#9999ff',edgecolor='white')
plt.bar(X,-Y2,facecolor='#ff9999',edgecolor='white')
for x,y in zip(X,Y1):
#ha:horizontal alignment
plt.text(x+0.1,y+0.05,'%.2f'%y,ha='center',va='bottom')
for x, y in zip(X, Y2):
# ha:horizontal alignment
plt.text(x + 0.1, -y - 0.05, '-%.2f' % y, ha='center', va='top')
plt.xlim(-.5,n)
plt.xticks(())
plt.ylim(-1.25,1.25)
plt.yticks(())
plt.show()
酷炫的条形图
等高线图
def f(x,y):
## the height function
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) ## 生成一个网格
## use plt.contourf to filling contours
## X,Y and value for (X,Y) point
plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot) ## cmap:颜色map value
## 不要把countourf写成countour,就没有颜色了
# 8是等高线把图像分成了多少部分,等高线的稠密程度
## use plt.contour to add contour lines
C = plt.contour(X,Y,f(X,Y),8,colors = 'black',linewidths=.5)
## adding label
plt.clabel(C,inline=True,fontsize=10)
plt.xticks(())
plt.yticks(())
plt.show()
火热的等高线
keypoint是理解栅格
类似热图的图片(从array生成)
##
import matplotlib.pyplot as plt
import numpy as np
# image data
## 每个点是一个像素点
a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
0.365348418405, 0.439599930621, 0.525083754405,
0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)
"""
for the value of "interpolation", check this:
http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html
for the value of "origin"= ['upper', 'lower'], check this:
http://matplotlib.org/examples/pylab_examples/image_origin.html
"""
plt.imshow(a,interpolation='nearest',cmap='bone',origin='lower')
plt.colorbar(shrink=0.9) ## p旁边的彩条
plt.xticks(())
plt.yticks(())
plt.show()
来个3D
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig) ## 添加3d坐标
## X,Y value
X =np.arange(-4,4,0.25)
Y =np.arange(-4,4,0.25)
X,Y = np.meshgrid(X,Y) # mesh底面
R = np.sqrt(X**2+Y**2)
# height value
Z = np.sin(R)
# print(R)
# print(Z)
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'),
edgecolor='black') # rstride cstride 行、列跨度
ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap='rainbow') ## zdir 从哪个轴压扁
## offset 压到该轴的哪个平面
ax.set_zlim(-2,2)
plt.show()