python可視化 matplotlib畫(huà)圖使用colorbar工具自定義顏色
python matplotlib畫(huà)圖使用colorbar工具自定義顏色 colorbar(draw colorbar without any mapple/plot)
自定義colorbar可以畫(huà)出任何自己想要的colorbar,自由自在、不受約束,不依賴(lài)于任何已有的圖(plot/mappable)。這里使用的是mpl.colorbar.ColorbarBase類(lèi),而colorbar類(lèi)必須依賴(lài)于已有的圖。
參數(shù)可以參考下面的描述->matplotlib:
class matplotlib.colorbar.ColorbarBase(ax, cmap=None, norm=None, alpha=None, values=None, boundaries=None, orientation=‘vertical’, ticklocation=‘a(chǎn)uto’, extend=‘neither’, spacing=‘uniform’, ticks=None, format=None, drawedges=False, filled=True, extendfrac=None, extendrect=False, label=’’)[source]
參數(shù)簡(jiǎn)單描述
ax :可用于設(shè)置colorbar的位置、長(zhǎng)、寬 norm :用于規(guī)范化?設(shè)置顏色條最大最小值 cmap:顏色(可參考本篇博文的最后部分——推薦色帶與自定義色帶) boundaries:要想使用extend,在norm之外,必須要有兩個(gè)額外的boundaries orientation:colorbar方向,躺平or垂直 extend:延伸方向(在norm之外colorbar可延伸) ticks:自定義各段的tick(記號(hào))給一個(gè)例子,首先定義一下橫縱坐標(biāo)的名稱(chēng),以及df_int:給一個(gè)例子,首先定義一下橫縱坐標(biāo)的名稱(chēng),以及df_int:
labels_int = [’A’, ’B’, ’C’, ’D’]variables_int = [’A’, ’B’, ’C’, ’D’]# x_normed_int 可以是一個(gè)4*4的數(shù)組,經(jīng)過(guò)歸一化的df_int = pd.DataFrame(, columns=variables_int, index=labels_int)
接下來(lái)就是畫(huà)圖了:
fig = plt.figure() ax = fig.add_subplot(111) cax = ax.matshow(df, interpolation=’nearest’, cmap=’GnBu’) fig.colorbar(cax) tick_spacing = 1 ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing)) ax.yaxis.set_major_locator(ticker.MultipleLocator(tick_spacing)) ax.set_xticklabels([’’] + list(df.columns)) ax.set_yticklabels([’’] + list(df.index)) plt.show()
其中:
cax = ax.matshow(df, interpolation=’nearest’, cmap=’GnBu’)
可以通過(guò)cmap修改,得到不同的顏色帶
最終可以看到結(jié)果如下圖:
到此這篇關(guān)于python可視化 matplotlib畫(huà)圖使用colorbar工具自定義顏色的文章就介紹到這了,更多相關(guān)python colorbar自定義顏色內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. idea給項(xiàng)目打war包的方法步驟2. idea配置jdk的操作方法3. IDEA插件EasyCode及MyBatis最優(yōu)配置步驟詳解4. django 鏈接多個(gè)數(shù)據(jù)庫(kù) 并使用原生sql實(shí)現(xiàn)5. Django程序的優(yōu)化技巧6. Python多線程操作之互斥鎖、遞歸鎖、信號(hào)量、事件實(shí)例詳解7. IntelliJ IDEA導(dǎo)入jar包的方法8. Python常用GUI框架原理解析匯總9. idea修改背景顏色樣式的方法10. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法
