[519]matplotlib(一)|Python中用matplotlib绘制热点图(heat map)

import numpy as np
 
# 高斯分布
mean = [0,0]
cov = [[0,1],[1,0]]
x, y = np.random.multivariate_normal(mean, cov, 10000).T
  • 使用NumPy 的 histogram2d 函数
from matplotlib import pyplot as plt

hist, xedges, yedges = np.histogram2d(x,y)
X,Y = np.meshgrid(xedges,yedges)
plt.imshow(hist)
plt.grid(True)
plt.colorbar()
plt.show()

在这里插入图片描述
改变插值方法:

plt.imshow(hist, interpolation='nearest')
plt.grid(True)
plt.colorbar()
plt.show()

在这里插入图片描述

  • 使用 matplotlib 的 hist2d 函数
plt.hist2d(x, y, bins=10)
plt.colorbar()
plt.grid()
plt.show()

在这里插入图片描述
改变bin大小:

plt.hist2d(x, y, bins=40)
plt.colorbar()
plt.grid()
plt.show()

在这里插入图片描述

  • 使用 matplotlib 的 pcolor 函数
plt.pcolor(hist)
plt.colorbar()
plt.grid()
plt.show()

在这里插入图片描述

  • 使用 matplotlib 的 matshow 函数
import numpy as np
import matplotlib.pyplot as plt
 
columns = ['A', 'B', 'C', 'D']
rows = ['1', '2', '3', '4']
 
data = np.random.random((4,4))
 
fig = plt.figure()
 
ax = fig.add_subplot(111)
 
cax = ax.matshow(data, interpolation='nearest')
fig.colorbar(cax)
 
ax.set_xticklabels([''] + columns)
ax.set_yticklabels([''] + rows)
 
plt.show()

在这里插入图片描述

  • 使用不同的颜色

可用颜色在http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps这里

from math import ceil
import numpy as np
 
# 高斯分布
mean = [0,0]
cov = [[0,1],[1,0]]
x, y = np.random.multivariate_normal(mean, cov, 10000).T
 
 
size = len(plt.cm.datad.keys())
all_maps = list(plt.cm.datad.keys())
 
fig, ax = plt.subplots(ceil(size/4), 4, figsize=(12,100))
 
counter = 0
for row in ax:
    for col in row:
        try:
            col.imshow(hist, cmap=all_maps[counter])
            col.set_title(all_maps[counter])
        except IndexError:
           break
        counter += 1
 
plt.tight_layout()
plt.show()

在这里插入图片描述

来源:http://blog.topspeedsnail.com/archives/707#more-707

  • 4
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值