Notes_matplotlib_二维等高线图和频次直方图

matplotlib 中的二维等高线图和频次直方图

import matplotlib.pyplot as plt
import numpy as np
plt.style.use('seaborn-white')
%matplotlib inline
def f(x,y):
    return np.sin(x)**10+np.cos(10+x*y)*np.cos(x)
x=np.linspace(0,5,50)
y=np.linspace(0,5,40)
X,Y=np.meshgrid(x,y)
#np.meshgrid,将坐标向量生成坐标网格,真的强!
#将坐标向量的笛卡尔积分别放入到不同的数组。
Z=f(X,Y)
plt.contour(Z,colors='k',
            linewidths=[1,2,3],#repeat
            linestyles=['-','--',':'],#repeat
            levels=20,
            alpha=0.5)
#注意是colors,不是color。
#levels参数:传递int则使用int个间隔,画int+1条线
#传递数组(值为等高的高度即Z值)则作为指定描绘的层(升序)?。
#cmap
<matplotlib.contour.QuadContourSet at 0x26ff25e7d90>

在这里插入图片描述
当图像只使用一种颜色时,默认虚线表示负数。

plt.contourf(X,Y,Z,levels=20,cmap=plt.cm.Blues)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x26ff26da0a0>

在这里插入图片描述

plt.imshow(Z,extent=[0,5,0,5],origin='lower',cmap=plt.cm.Blues)
plt.colorbar()
plt.axis(aspect='image')
(0.0, 5.0, 0.0, 5.0)

在这里插入图片描述

plt.imshow()plt.contour都用origin与extent参数:
origin确定Z[0,0]的位置,extent确定坐标轴[xmin,xmax,ymin,ymax]。
前者:origin默认’upper’(左上)后者默认’lower’(左下)且只在X,Y没有给出时用。
plt.imshow()不接受X,Y。只有Z,或所谓的图像信息:(M,N,RGB元组).

contours=plt.contour(X,Y,Z,levels=3,colors='k')
#plt.contour返回了一个ContourSet对象即cs。
plt.clabel(contours,inline=True,fontsize=8)#该函数第一个参数需要cs
plt.imshow(Z,extent=[0,5,0,5],origin='lower',cmap=plt.cm.Blues,alpha=0.5)
plt.colorbar()
#这是在一张图上画了三次:画带标签的线,画图,画colorbar。
<matplotlib.colorbar.Colorbar at 0x26ff37e9cd0>

在这里插入图片描述

一维频次直方图

x=np.random.randn(1000,3)
plt.hist(x,bins=10,#记住,这个是频次分布直方图,才能理解x轴与y轴的含义。
         density=True,#将返回的元组第一个元素改为概率密度(就是对各个bin内个数归一化)。
                      #具体方法要去查看docstring(没看懂...),False则返回各个bin内的个数。
                      #y即histogram axis也变为了概率密度。
         alpha=0.5,
         histtype='barstacked',#bar是传统的,barstacked,是可重叠,
                               #step是边框,叠层,stepfilled填充叠层,与alpha搭配效果好!
         color=['c','b','r'],#多维x时传递列表,没有cmap参数
         edgecolor='g',
         align='mid',#'left'','right','mid'可选,是bar的中心对齐bin的位置:left对齐左边界
         orientation='vertical',#方向,{'horizontal','vertical'}
         log=False,#将histogram axis变为指数。
         label=['ass','bii'],#按照顺序获得label,(因为使用了yield,所以可以不全给出)
         stacked=False,#对histtype为’step'时处理,重叠。
         )
#还包括data参数。即权重weights
#返回元组,(个数或者概率密度,the edges of the bins,)

plt.legend()
<matplotlib.legend.Legend at 0x26ff382efd0>

在这里插入图片描述

counts,bin_edges=np.histogram(x,bins=10)
counts#使用的是numpy中的函数
array([  7,  48, 197, 449, 732, 735, 536, 204,  79,  13], dtype=int64)
bin_edges
#只想知道每个区间的样本数,而不画图,使用np.histogram.返回两个数组。
array([-3.31731619, -2.65909454, -2.00087288, -1.34265123, -0.68442957,
       -0.02620792,  0.63201374,  1.29023539,  1.94845705,  2.6066787 ,
        3.26490036])
a=plt.hist(x)
#关注其返回值。
a
([array([  1.,  14.,  60., 164., 255., 237., 174.,  64.,  26.,   5.]),
  array([  5.,  17.,  66., 157., 221., 245., 194.,  66.,  24.,   5.]),
  array([  1.,  17.,  71., 128., 256., 253., 168.,  74.,  29.,   3.])],
 array([-3.31731619, -2.65909454, -2.00087288, -1.34265123, -0.68442957,
        -0.02620792,  0.63201374,  1.29023539,  1.94845705,  2.6066787 ,
         3.26490036]),
 <a list of 3 Lists of Patches objects>)

在这里插入图片描述

二维频次直方图

#先创建一个多元高斯分布:
mean=[0,0]
cov=[[1,1],[1,2]]
x,y=np.random.multivariate_normal(mean,cov,10000).T
plt.hist2d(x,y,#一维数组
           bins=30,#int 或者[int,int]分别对x,y.或者array对x,y相同的不同分区,或者[array,array]
           cmap='Blues',
           range=[[-2,2],[-2,2]],#确定x,y边界,shape(2,2)
           density=True,#同plt.hist
           cmin=0.01,#过滤掉低于该值的bins。返回值也会被设为np.nan.cmax同
           )
#同样有data参数。及权重weights。
plt.colorbar().set_label('counts in bin')
#colorbar的标签,或者:plt.colorbar(label='counts in bin')

在这里插入图片描述

plt.hexbin(x,y,gridsize=30,cmap='autumn_r')
plt.colorbar(label='counts in bin')
#六边形的bin
#参数详见docstring.
#.PolyCollection
<matplotlib.colorbar.Colorbar at 0x1dd2e981b50>

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值