社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

【基础】学习笔记53-Python3 matplotlib绘图-热力图2

Alyna_C • 4 年前 • 123 次点击  

热力图2

运行结果为:


代码如下:

# 随机热力图:imshow

import matplotlib.pyplot as plt

import numpy as np

import seaborn as sns

plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']

'''

# ===========随机热力图==========

x = np.random.rand(10)

y = np.random.rand(10)

z = (np.random.rand(9000000) + np.linspace(0, 1, 9000000)

).reshape(3000, 3000)  # reshape函数将原矩阵A,重组为新矩阵B

im = plt.imshow(z, interpolation='nearest', cmap='hot',

extent=(np.amin(x), np.amax(x), np.amin(

y), np.amax(y))  # 设置坐标轴范围

)

plt.colorbar(im, shrink=0.5)  # 设置Bar

plt.show() '''

def Imshow_heatmap(data, xlabel, ylabel):  # imshow绘制矩阵随机热力图

# 使用figure

# plt.figure(facecolor='w')

#plt.subplot(111, position=[0.1, 0.15, 0.8, 0.8])

plt.yticks(range(len(ylabel)), ylabel)

plt.xticks(range(len(xlabel)), xlabel)

'''

# 使用Figure显式的创建Axes

fig = plt.figure(facecolor='w')

ax = fig.add_subplot(111, position=[0.1, 0.15, 0.8, 0.8])

ax.set_yticks(range(len(ylabel)))

ax.set_yticklabels(ylabel)  # 用ylabel替代y坐标值

ax.set_xticks(range(len(xlabel)))

ax.set_xticklabels(xlabel)  # 用xlabel替代x坐标值'''

# 求data数组的最小值和最大值

vmax, vmin = data[0][0], data[0][0]

for i in data:

if min(i) < vmin:

vmin = min(i)

if max(i) > vmax:

vmax = max(i)

map = plt.imshow(data, interpolation='nearest',

cmap='summer', vmin=vmin, vmax=vmax)

plt.colorbar(map, shrink=0.5)

plt.show()

if __name__ == "__main__":

data = np.random.rand(10, 10)

xlabel = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']

ylabel = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

Imshow_heatmap(data, xlabel, ylabel)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/105614
 
123 次点击