引包:
import numpy as np
import matplotlib.pyplot as plt
#guagua_txt
plt.style.use('ggplot')
x=np.random.randn(200)
y=np.random.randn(200)*0.5+x
# print(y)
margin_border=0.1
width=0.6
margin_between=0.04
height=0.2
# 图大坐标
left_s=margin_border
bottom_s=margin_border
height_s=width
width_s=width
# 图小1坐标
left_x=margin_border
bottom_x=margin_border+margin_between+width
height_x=height
width_x=width
#图小2坐标
left_y=margin_border+width+margin_between
bottom_y=margin_border
height_y=width
width_y=height
# 生成一个画布
fig=plt.figure(1,figsize=(8,8))
rect_s=[left_s,bottom_s,width_s,height_s]
rect_x=[left_x,bottom_x,width_x,height_x]
rect_y=[left_y,bottom_y,width_y,height_y]
# 生成图形
axScatter=plt.axes(rect_s)
axHisX=plt.axes(rect_x)
axHisy=plt.axes(rect_y)
# 去掉图小的两个坐标
axHisX.set_xticks([])
axHisy.set_yticks([])
#画图大
axScatter.scatter(x,y,c='b')
#固定箱体的宽度
bin_width=0.25
# 合理设置x轴y轴,使图形美观
#求出x与y的绝对值的最大值 用np.max([ ]),,np.fabs()是求绝对值
xymax=np.max([np.max(np.fabs(x)),np.max(np.fabs(y))])
#精确条形图的宽度值
lim=int(xymax/bin_width+1)*bin_width
# 设置x,y轴的最大最小值
axScatter.set_xlim(-lim,lim)
axScatter.set_ylim(-lim,lim)
# 设置直方图的步长从-lim到lim 隔bin_width
bins=np.arange(-lim,lim+bin_width,bin_width)
# 画出两个直方图
axHisX.hist(x,bins=bins)
axHisy.hist(y,bins=bins,orientation='horizontal')
# 摆放方式为= ‘horizontal’
# 设置x,y轴的最大最小值使与图大的数据对应
axHisX.set_xlim(axScatter.get_xlim())
axHisy.set_ylim(axScatter.get_ylim())
# print(axScatter.get_xlim())
# 标题
axHisy.set_title("histogram_2",loc='left')
axHisX.set_title("histogram_1",loc='left')
axScatter.set_title("Scatter plot",loc='left')
fig.suptitle('guagua_txt', fontsize=24, x=0.4,y=0.95, horizontalalignment='left', va='bottom')
plt.show()
运行效果图: