python matplotlib imshow熱圖坐標替換/映射實例
今天遇到了這樣一個問題,使用matplotlib繪制熱圖數(shù)組中橫縱坐標自然是圖片的像素排列順序,
但是這樣帶來的問題就是畫出來的x,y軸中坐標點的數(shù)據(jù)任然是x,y在數(shù)組中的下標,
實際中我們可能期望坐標點是其他的一個范圍,如圖:

坐標點標出來的是實際數(shù)組中的下標,而我希望縱坐標是頻率,橫坐標是其他的范圍
plt.yticks(np.arange(0, 1024, 100), np.arange(10000, 11024, 100)) #第一個參數(shù)表示原來的坐標范圍,100是每隔100個點標出一次 #第二個參數(shù)表示將展示的坐標范圍替換為新的范圍,同樣每隔100個點標出一次 plt.xticks(np.arange(0, 2000, 500), np.arange(0, 50000, 500)) #同理將x軸的表示范圍由(0,2000)擴展到(0,50000)每隔500個點標出一次

完成!
補充知識: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 pass語句作用和Python assert斷言函數(shù)的用法
這篇文章主要介紹了Python pass語句作用和Python assert斷言函數(shù)的用法,文章內容介紹詳細具有一定的參考價值,需要的小伙伴可以參考一下,希望對你有所幫助2022-03-03
淺述python中argsort()函數(shù)的實例用法
本篇文章主要介紹了淺述python中argsort()函數(shù)的實例用法,詳細的介紹了argsort()函數(shù)的用法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
解決python執(zhí)行較大excel文件openpyxl慢問題
這篇文章主要介紹了解決python執(zhí)行較大excel文件openpyxl慢問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05

