Python Matplotlib Imshow热图坐标映射与替换方法

拾柒
拾柒
发布时间:2026-10-13| 5人浏览过
原创

本文介绍如何在Python中使用Matplotlib绘制热图时,将默认的像素下标坐标替换为实际物理范围。通过调整xticks和yticks参数,实现横纵坐标轴的数值映射,使图表更贴合实际应用场景。

Matplotlib热图坐标映射问题

在使用Matplotlib绘制热图时,默认的横纵坐标通常对应数组的像素排列顺序。

这会导致X轴和Y轴的刻度显示为数组中的索引下标,而非实际的业务数据值。

在实际应用中,我们往往希望坐标轴显示特定的数值范围,例如频率或其他物理量。

默认情况下,坐标轴标注的是数组内部的下标,但我们需要将其映射为更有意义的范围,如纵轴表示频率,横轴表示其他变量。

坐标轴替换实现代码


plt.yticks(np.arange(0, 1024, 100), np.arange(10000, 11024, 100))
#第一个参数表示原来的坐标范围,100是每隔100个点标出一次
#第二个参数表示将展示的坐标范围替换为新的范围,同样每隔100个点标出一次
plt.xticks(np.arange(0, 2000, 500), np.arange(0, 50000, 500)) 
#同理将x轴的表示范围由(0,2000)扩展到(0,50000)每隔500个点标出一次

通过简单的参数调整即可完成坐标轴的自定义映射。

Scatter函数Cmap用法补充

补充知识:Matplotlib中plt.scatter()函数的cmap参数用法。

以下直接展示相关代码实现。


import numpy as np
import matplotlib.pyplot as plt


# Have colormaps separated into categories:
# http://matplotlib.org/examples/color/colormaps_reference.html
cmaps = [('Perceptually Uniform Sequential', [
      'viridis', 'plasma', 'inferno', 'magma']),
     ('Sequential', [
      'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
      'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
      'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']),
     ('Sequential (2)', [
      'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink',
      'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia',
      'hot', 'afmhot', 'gist_heat', 'copper']),
     ('Diverging', [
      'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu',
      'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']),
     ('Qualitative', [
      'Pastel1', 'Pastel2', 'Paired', 'Accent',
      'Dark2', 'Set1', 'Set2', 'Set3',
      'tab10', 'tab20', 'tab20b', 'tab20c']),
     ('Miscellaneous', [
      'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern',
      'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv',
      'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar'])]


nrows = max(len(cmap_list) for cmap_category, cmap_list in cmaps)
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))


def plot_color_gradients(cmap_category, cmap_list, nrows):
  fig, axes = plt.subplots(nrows=nrows)
  fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99)
  axes[0].set_title(cmap_category + ' colormaps', fontsize=14)

  for ax, name in zip(axes, cmap_list):
    ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
    pos = list(ax.get_position().bounds)
    x_text = pos[0] - 0.01
    y_text = pos[1] + pos[3]/2.
    fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)

  # Turn off *all* ticks & spines, not just the ones with colormaps.
  for ax in axes:
    ax.set_axis_off()


for cmap_category, cmap_list in cmaps:
  plot_color_gradients(cmap_category, cmap_list, nrows)

#十分类散点图绘制
randlabel = np.random.randint(0,1,10)
randdata = np.reshape(np.random.rand(10*2),(10,2))


cm = plt.cm.get_cmap('RdYlBu')
z = randlabel
sc = plt.scatter(randdata[:,0], randdata[:,1], c=z, vmin=0, vmax=10, s=35,edgecolors='k', cmap=cm)
plt.colorbar(sc)
plt.show()

以上关于Python Matplotlib热图坐标替换与映射的内容分享完毕,希望能为读者提供参考,欢迎继续支持本站。

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。