python matplotlib imshow热图坐标替换/映射实例

2020-09-24 0 242

今天遇到了这样一个问题,使用matplotlib绘制热图数组中横纵坐标自然是图片的像素排列顺序,

但是这样带来的问题就是画出来的x,y轴中坐标点的数据任然是x,y在数组中的下标,

实际中我们可能期望坐标点是其他的一个范围,如图:

python matplotlib imshow热图坐标替换/映射实例

坐标点标出来的是实际数组中的下标,而我希望纵坐标是频率,横坐标是其他的范围

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个点标出一次

python matplotlib imshow热图坐标替换/映射实例

完成!

补充知识: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 imshow热图坐标替换/映射实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自学编程网。

遇见资源网 Python python matplotlib imshow热图坐标替换/映射实例 http://www.ox520.com/24973.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务