python_matplotlib DAY_22(4)scatter-hist图画

学习内容
scatter—hist联合图

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mp

x = np.random.randn(200)
y = x + np.random.randn(200) * 0.5#产生正相关的随机数

margin_border = 0.1
width = 0.6
margin_between = 0.02
height = 0.2
#因为需要产生三幅图,使用到plt.axes([left,bottom,width,height])
#而三个数据均在0-1之间,所以这个是按比例放缩的一个数据

plt.style.use('ggplot')#填充效果
left_down = margin_border
bottom_down = margin_border
height_down = width
width_down = width#图中左下方的图像数据

left_up = margin_border
bottom_up = margin_border + width + margin_between
height_up = height
width_up = width#最上面图像数据

left_right = margin_border + width + margin_between
bottom_right = margin_border
height_right = width
width_right = height#最右面图像的数据

plt.figure()#产生正方形,8×8的数据

rect_up = [left_up,
           bottom_up, width_up,
           height_up]
rect_down = [left_down,
             bottom_down,
             width_down,
             height_down]
rect_right = [left_right,
              bottom_right, width_right,
              height_right]
axscatter_down = plt.axes(rect_down)
axhist_up = plt.axes(rect_up)
axhist_right = plt.axes(rect_right)
#上面通过列表写入数据,调用到plt.axes()函数中去
axhist_right.set_yticks([])
axhist_up.set_xticks([])#隐藏重叠轴线
axscatter_down.scatter(x, y)
bin_width = 0.25#hist需要bins设置,以下是设置bins(分搁块)
xymax = np.max([np.max(np.fabs(x)), np.max(np.fabs(y))])
lim = int(xymax / bin_width + 1) * bin_width#通过求出最大的bin宽,估算出整个长度
axhist_up.set_xlim(-lim, lim)
axhist_right.set_ylim(-lim, lim)#设置长度
bins = np.arange(-lim, lim + bin_width, bin_width)

axhist_up.hist(x, bins=bins)
axhist_right.hist(y, bins=bins,orientation='horizontal')
#水平作图,需要加上horizontal,表示垂直

plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值