カラーバーのmatplotlibの中にカラーマップのカラーバーとリボン

カラーマップ

唯一の3つの主要な書類、

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

カラーバーの設定範囲はvmin - vmax、あなたが使用することができ、シリーズの色を設定するReds,Bluesより、以下のリンクや写真の
カラーマップがhttps://www.matplotlib.org.cn/tutorials/colors/colormaps.htmlをリンクを参照することができました

  • 使用します。
colorVal = scalarMap.to_rgba(i)

カラーバー

参考リンク:https://matplotlib.org/2.0.2/examples/api/colorbar_only.html

  • 新しい軸ドメイン ax = fig.add_axes([left, bottom, width, height])
  • 明確な cmap, norm
  • 新しいカラーバー

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 ビュー20000 +

おすすめ

転載: blog.csdn.net/weixin_38102912/article/details/104088000