import matplotlib.pyplot as plt
import numpy as np
n = 1024
X = np.random.normal(0,1,n) #每一个点的X值
Y = np.random.normal(0,1,n) #每一个点的Y值
T = np.arctan2(Y, X) #for color value
plt.scatter(X, Y, s = 75, c = T , alpha = 0.5)
plt.xlim(-1.5,1.5)
plt.xticks(()) #隐藏x坐标轴
plt.ylim(-1.5,1.5)
plt.yticks(()) #隐藏y坐标轴
plt.show()
.scatter
的参数为
plt.scatter(x, y, c = None, marker = 'o', cmap = None, vmin = None, vmax = None, alpha = None, linewidths = None, verts = None, edgecolors = None, hold = None, data = None, **kwargs)
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')
plt.xlim(-.5,n)
plt.xticks(())
plt.ylim(-1.25,1.25)
plt.yticks(())
#在柱体上方和下方标注数值
for x, y in zip(X ,Y1):
#ha: horizontal alignment 横向对齐
#va: vertical alignment 纵向对齐
plt.text(x + 0.04, y + 0.05, '%.2f'% y, ha = 'center', va = 'bottom')
for x, y in zip(X ,Y2):
#ha: horizontal alignment
#va: vertical alignment
plt.text(x + 0.04, -y - 0.05, '%.2f'% y, ha = 'center', va = 'top')
def f(x, y):
# the high 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)
#use plt.contour to add contour line
C = plt.contour(X, Y, f(X, Y), 8,colors = 'black', linewidth =.5)
plt.clabel(C, inline = True, fontsize = 10)
plt.xticks(())
plt.yticks(())
.meshgrid
在二维平面中将每一个x和每一个y对应起来,编织成栅格
.contourf
用于填充颜色,位置参数分别为:X,Y,f(X,Y)。8
代表等高线的密集程度。颜色设置为color map中的暖色组,也可设置为cool
.contour
用于绘制等高线
.clabel
添加数值,inline
控制是否将label画在线里。
a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
0.365348418405, 0.439599930621, 0.525083754405,
0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)
plt.imshow(a, interpolation='nearest', #选择出图方式
cmap='bone',
origin='lower') #选择原点的位置,可设置为'upper'
plt.colorbar(shrink=.92) #压缩colorbar的长度
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
# X, Y value
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y) # x-y 平面的网格
R = np.sqrt(X ** 2 + Y ** 2)
# height value
Z = np.sin(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow') )
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap='rainbow')
ax.set_zlim(-2, 2)
plt.show()
rstride
和cstride
分别代表row和column的跨度
cmap
参数有多种设置方式
zdir
选择投影方向
2018.08.07 整理于莫烦python教程
https://morvanzhou.github.io/tutorials/data-manipulation/plt/