Python三維繪圖--Matplotlib colorbar生成


在這里插入圖片描述
Matplotlib 使用colorbar來設置色階條:

colorbar(**kwargs) colorbar(mappable, **kwargs) colorbar(mappable, cax=cax, **kwargs) colorbar(mappable, ax=ax, **kwargs) #需要制定顏色樣式圖,所要畫色階條的軸 #ref:https://matplotlib.org/gallery/color/colorbar_basics.html#sphx-glr-gallery-color-colorbar-basics-py 

下面是一個例子:

import numpy as np import matplotlib.pyplot as plt #生成繪圖數據 N = 100 x, y = np.mgrid[:100, :100] Z = np.cos(x*0.05+np.random.rand()) + np.sin(y*0.05+np.random.rand())+2*np.random.rand()-1 # mask out the negative and positive values, respectively Zpos = np.ma.masked_less(Z, 0) #小於零的部分 Zneg = np.ma.masked_greater(Z, 0) #大於零的部分 fig, (ax1, ax2, ax3) = plt.subplots(figsize=(13, 3), ncols=3) pos = ax1.imshow(Zpos, cmap='Reds', interpolation='none') fig.colorbar(pos, ax=ax1) #這里使用colorbar來制定需要畫顏色棒的圖的軸,以及對應的cmap,與pos對應 neg = ax2.imshow(Zneg, cmap='Blues_r', interpolation='none') fig.colorbar(neg, ax=ax2) pos_neg_clipped = ax3.imshow(Z, cmap='jet', vmin=-2, vmax=2,interpolation='none') #-2,2的區間 fig.colorbar(pos_neg_clipped, ax=ax3) plt.show() #ref: # https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.colorbar # https://matplotlib.org/gallery/color/colorbar_basics.html#sphx-glr-gallery-color-colorbar-basics-py 

在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM