Matplotlib绘制柱状、散点、直方、箱型; Subplots绘制子图;共享轴线

Matplotlib介绍

%matplotlib inline #魔法函数,有了它,直接就可以显示结果,不需要plt.show()
import numpy as np
import matplotlib.pyplot as plt
X=np.linspace(0,2*np.pi,100)  #均匀地划分数据
Y=np.sin(X)
Y1=np.cos(X)
plt.plot(X,Y)  #向其中添加元素
plt.plot(X,Y1) #向其中添加元素
plt.show()  #数据可视化
plt.subplot(211)   # 等价于subplot(2,1,1) 2行1列中第1行
plt.plot(X,Y)
plt.subplot(212)   # 等价于subplot(2,1,2)2行1列中第2行
plt.plot(X,np.cos(X),color = "r")

柱状图

data = [5,25,50,20]
plt.bar(range(len(data)),data)  # 竖向柱状图,range()表示遍历data的长度:0,1,2,3
plt.bar(range(len(data)),data)  # 横向的柱状图
data=[[5,25,50,20],[4,23,51,17],[6,22,52,9]]   # 三行四列在图中分成四组,每列为一组,每组三个数据
X=np.arange(4)   # 横轴长度为0-4
plt.bar(X+0.00,data[0],color = "b",width = 0.25,label = 'A')  # label = 'A'表示图例
plt.bar(X+0.25,data[1],color = "g",width = 0.25,label = 'B')
plt.bar(X+0.50,data[2],color = "r",width = 0.25,label = 'C')   # X+0.50表示右移0.5
plt.legend()  # 给图像加上图例
plt.bar(X,data[0],color = "b",width = 0.25,label = 'A')
plt.bar(X,data[1],color = "g",width = 0.25,bottom=data[0],label = 'B')   # bottom=data[0] 表示其下层为data中下标为0的数据
plt.bar(X,data[2],color = "r",width = 0.25,bottom=data[0]+np.array(data[1]),label = 'C')   # bottom=data[0]+np.array(data[1]) 表示其下层为data中下标为0和下标为1的数据,需要至少一个np.array()函数

散点图

N=50
X=np.random.rand(N)
Y=np.random.rand(N)
colors = np.random.randn(N)
area = np.pi*(20*np.random.rand(N))**2 # 调整大小
plt.scatter(X,Y,c=colors,alpha=0.5,s=area )   # alpha=0.5表示透明度

直方图

a=np.random.randn(10000)   # a用于指定条状图的分布情况
plt.hist(a,bins = 200)   # 建立直方图,bins用于设置条状图数量
# plt.xlim(-10,10)     # 设置x轴长度
plt.ylim(0,150)     # 设置y轴长度
plt.title('A')    # 设置标题

箱型图

x=np.random.randint(20,100,size=(10,3))  # 创建20-100之间的随机整数,创建3组数据,每组数据10个数字
plt.boxplot(x)   # 创建箱型图
plt.ylim(0,120)   # 设置y轴长度
plt.xticks([1,2,3],['A','B','C'])  # 将箱型图对应的x轴的下标由[1,2,3]改为['A','B','C']

颜色

plt.subplots(facecolor="teal")   # 为图形添加背景色,这句代码要放在其他元素的前面才能对图形起作用

文字

plt.xlabel('Group')  # 横轴名称
plt.ylabel('Number')   # 纵轴名称
plt.text(0.5,38,"Text")   #在生成的图像中添加文字Text,“0.5,38”代表横纵坐标

Subplots绘制子图

将画布划分成均等的几个部分,分别添加元素,绘制图像
np.random.seed(90)  # 使产生的随机数相同,随机数种子的参数只是确定一下随机数的起始位置,可随意分配
n_bins=10  # 10个方形图
x=np.random.randn(1000,3)  # 3组,每组1000个样本数据,即1000行3列
fig,axes = plt.subplots(nrows=2,ncols=2,facecolor='darkslategray')  # fig代表绘图窗口(Figure);ax代表这个绘图窗口上的坐标系(axis)
ax0,ax1,ax2,ax3=axes.flatten() # ax由n*m的Axes组展平成1*nm的Axes组,否则不能直接使用ax[i],要写成axs[0, 0]的形式


colors=['red','tan','lime']
ax0.hist(x,n_bins,normed=1,histtype='bar',color=colors,label=colors)  # normed=1将所有数据进行归一化 ,histtype='bar'正常的方形图格式
ax0.legend(prop={
    
    'size':10}) # 显示图例,size设置大小
ax0.set_title('bars with legend')
 
ax1.hist(x,n_bins,normed=1,histtype='bar',stacked=True)  # stacked=True将图形叠在一起
ax1.set_title('stacked bar')

ax2.hist(x,n_bins,histtype='step',stacked=True,fill=False) # fill=False图形不填充颜色
ax2.set_title('stack step(unfilled)')

x_multi =[np.random.randn(n) for n in [10000,5000,2000]]  #n=10000,n=5000,n=2000
ax3.hist(x_multi,n_bins,histtype='bar')
ax3.set_title('different sample sizes')


fig.tight_layout()  # 使图像位置更整齐规范,避免重叠
共享x轴、y轴
np.random.rand(4,3,2)   # 4个3行2列的数组
np.random.rand(1,3,2) 相当于 np.random.rand(3,2)
np.random.seed(90)
N_points=100000
n_bins =20
a=np.random.randn(N_points)
b=1.4*np.random.randn(100000)+5   # 正态分布服从(5,1.4^2)

fig,axs = plt.subplots(1,2,sharey=True,tight_layout=True) # sharey=True共享y轴

axs[0].hist(a,bins=n_bins)
axs[1].hist(b,bins=n_bins)

猜你喜欢

转载自blog.csdn.net/xiaokeaiuiya/article/details/108737814