(1)柱面图plt.bar
# 柱面图plt.bar
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
n = 8
X = np.arange(n)+1
#X是1,2,3,4,5,6,7,8,柱的个数
# numpy.random.uniform(low=0.0, high=1.0, size=None), normal
#uniform均匀分布的随机数,normal是正态分布的随机数,0.5-1均匀分布的数,一共有n个
Y1 = np.random.uniform(0.5,1.0,n)
Y2 = np.random.uniform(0.5,1.0,n)
#width:柱的宽度,facecolor:柱面的颜色,edgecolor:柱的边界颜色
plt.bar(X,Y1,width = 0.35,facecolor = 'blue',edgecolor = 'red')
# 可以再添加一些柱条,添加-号可以将柱面反过来
plt.bar(X+0.35,-Y2,width = 0.35,facecolor = 'yellow',edgecolor = 'black')
# 水平柱状图plt.barh,属性中宽度width变成了高度height
#给图加text
for x,y in zip(X,Y1):
plt.text(x+0.3, y+0.05, '%.2f' % y, ha='center', va= 'bottom')
for x,y in zip(X,Y2):
plt.text(x+0.6, -y-0.05, '%.2f' % y, ha='center', va= 'bottom')
plt.ylim(-1.25,+1.25)
plt.show()
(2)散点图plt.scatter
#散点图plt.scatter
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
n=1000
#rand 均匀分布和 randn高斯分布
x=np.random.randn(1,n)
y=np.random.randn(1,n)
#c:散点的颜色,s:散点的大小,alpha:是透明程度
plt.scatter(x,y,c='red',s=25,alpha=0.4,marker='o')
plt.show()
(3)坐标系填充颜色plt.pcolormesh
# plt.pcolormesh给坐标填充颜色,分开填色
import numpy as np
import matplotlib.pyplot as plt
h = 0.5
x_min = 0
x_mid = 5
x_max = 10
y_min = 0
y_mid = 5
y_max = 10
xx1, yy1 = np.meshgrid(np.arange(x_min, x_mid, h),
np.arange(y_min, y_mid, h))
xx2, yy2 = np.meshgrid(np.arange(x_mid, x_max, h),
np.arange(y_mid, y_max, h))
z1 = np.ones(xx1.shape)
z2 = np.zeros(xx2.shape)
# 分开填色
plt.pcolormesh(xx1,yy1,z1,cmap=plt.cm.Paired)
plt.pcolormesh(xx2,yy2,z2,cmap=plt.cm.Paired)
plt.show()
同时填色
# 同时填色
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA'])
h = 0.5
x_min = 0
x_mid = 5
x_max = 10
y_min = 0
y_mid = 5
y_max = 10
xx3, yy3 = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
z3 =np.zeros(xx3.shape)
m,n = xx3.shape
z3[:m/2] = 1
plt.pcolormesh(xx3,yy3,z3,cmap=cmap_light)
plt.show()