matplotlib中的色条colormap 及色带colorbar

colormap

关键只有三个语句,

  • 初始化
jet = cm = plt.get_cmap('Reds')
cNorm = colors.Normalize(vmin=0, vmax=5)
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet)

将色条设置范围vmin - vmax, 并设置色条系列,也可以使用Reds,Blues更多参考以下链接及图片
其中 colormap 可参考链接 https://www.matplotlib.org.cn/tutorials/colors/colormaps.html

  • 使用:
colorVal = scalarMap.to_rgba(i)

color bar

参考链接: https://matplotlib.org/2.0.2/examples/api/colorbar_only.html

  • 新建轴域 ax = fig.add_axes([left, bottom, width, height])
  • 明确 cmap, norm
  • 新建colorbar

cb = matplotlib.colorbar.ColorbarBase(ax, cmap=None, norm=None, alpha=None, values=None, boundaries=None, orientation=‘vertical’, ticklocation=‘auto’, extend=‘neither’, spacing=‘uniform’, ticks=None, format=None, drawedges=False, filled=True, extendfrac=None, extendrect=False, label=’’)

%matplotlib notebook
import matplotlib
from matplotlib import pyplot as plt

#%matplotlib tk
from matplotlib import rcdefaults
rcdefaults()
import numpy as np
import pandas as pd
jet = cm = plt.get_cmap('Reds') 
#这里可以选择色条方案
#e.g. Greys, 'Blues'
cNorm = matplotlib.colors.Normalize(vmin=0, vmax=5)
scalarMap = matplotlib.cm.ScalarMappable(norm=cNorm, cmap=jet)
print(scalarMap.get_clim()) 

fig = plt.figure(figsize=(8, 3))
#ax = fig.add_subplot(111)
# 这里为设置并排效果 不采用add_subplot, 而是 add_axes, 其中关于 这二者的区别详见 matplotlib基础教程
ax = fig.add_axes([0.0, 0.0, 0.60, 0.8])
#------绘图---------------------
x = np.arange(10, 15, 1)
y = np.arange(10, 15, 1)

for i in range(5):
    colorVal = scalarMap.to_rgba(i)
    print('value',i,'color value', colorVal)
    ax.plot(x, y + i, c = colorVal )  
    
#----设置color bar----------
ax_bar = fig.add_axes([0.6, 0.0, 0.05, 0.8])
bounds = [0,1,2,3,4,5]
# to use 'extend', you must  specify two extra boundaries: boundaries=[-1] + bounds + [6]
# extend = 'both',
cb1 = matplotlib.colorbar.ColorbarBase(ax_bar, cmap=jet,
                                norm=cNorm,
                                orientation='vertical',
                                boundaries=[-1] + bounds + [6],
                                extend = 'both',
                                ticks = bounds,
                                spacing = 'uniform')
cb1.set_label('Color bar',fontsize = 8)

plt.show()

(0.0, 5.0)
value 0 color value (1.0, 0.9607843137254902, 0.9411764705882353, 1.0)
value 1 color value (0.9913725490196079, 0.7913725490196079, 0.7082352941176471, 1.0)
value 2 color value (0.9874509803921568, 0.5411764705882353, 0.41568627450980394, 1.0)
value 3 color value (0.9466666666666667, 0.26823529411764707, 0.19607843137254902, 1.0)
value 4 color value (0.7364705882352941, 0.08, 0.10117647058823528, 1.0)


C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py:522: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
  max_open_warning, RuntimeWarning)



<IPython.core.display.Javascript object>

在这里插入图片描述

发布了36 篇原创文章 · 获赞 0 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_38102912/article/details/104088000