温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python中matplotlib imshow热图坐标替换/映射的示例分析

发布时间:2021-07-02 10:11:21 来源:亿速云 阅读:150 作者:小新 栏目:开发技术

小编给大家分享一下python中matplotlib imshow热图坐标替换/映射的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

今天遇到了这样一个问题,使用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热图坐标替换/映射的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI