Matplotlib绘制矩阵图,plt.matshow/imshow 与 ax.pcolor(pcolormesh)方法的使用

文章目录

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap

#data
np.random.seed(42)
data = np.random.rand(4, 4)

plt.matshow

可以把下面的matshow换成imshow。

离散colorbar
python 复制代码
plt.matshow(data, cmap=cMap)#离散colorbar  cMap是前面自己定义的。
plt.colorbar(fraction=0.025)#colorbar的大小
plt.show()
连续colorbar

有哪些Matplotlib内置的colorbar呢?大家可以参考https://matplotlib.org/stable/users/explain/colors/colormaps.html。

python 复制代码
plt.matshow(data,cmap="gist_rainbow")
plt.colorbar(fraction=0.025)#colorbar的大小
plt.show()

ax.pcolor

可以把下面的pcolor换成pcolormesh。

简单应用
python 复制代码
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=cMap)
cbar = plt.colorbar(heatmap)
综合应用

这里会自定义x/y轴的文字,以及右边的那个图例。

python 复制代码
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=cMap)

#legend
cbar = plt.colorbar(heatmap)
cbar.ax.set_yticklabels(['0','1','2','>3'])
cbar.set_label('# of contacts', rotation=270)

# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False)
ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False)
ax.invert_yaxis()

#labels
column_labels = list('ABCD')
row_labels = list('WXYZ')
ax.set_xticklabels(column_labels, minor=False)
ax.set_yticklabels(row_labels, minor=False)

plt.show()
相关推荐
㳺三才人子4 天前
初探 Data Analysis - Matplotlib
python·plotly·pandas·matplotlib
菜冻鱼5 天前
Python-sklearn-特征工程
开发语言·人工智能·python·机器学习·numpy·matplotlib·sklearn
cui_ruicheng14 天前
Python数据分析(十一):数据可视化与 Matplotlib
python·信息可视化·数据分析·matplotlib
编程阿峰16 天前
Python 环境配置(二)安装jupyter、matplotlib、numpy库
开发语言·python·jupyter·numpy·matplotlib
人工智能时代 准备好了吗17 天前
Python + Jinja2 + Matplotlib:AI回答采集报告自动化生成实现
人工智能·python·matplotlib
chouchuang20 天前
day-044-Matplotlib进阶-高级图表
matplotlib
你想知道什么?1 个月前
Matplotlib学习笔记
笔记·学习·matplotlib
风流 少年1 个月前
数据可视化:matplotlib、pyecharts、panda、seaborn
数据挖掘·数据分析·matplotlib
fai厅的秃头姐!1 个月前
Matplotlib
matplotlib
许彰午1 个月前
78_Python数据可视化matplotlib
python·信息可视化·matplotlib