最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python利用imshow制作自定義漸變填充柱狀圖(colorbar)

 更新時(shí)間:2020年12月10日 11:50:29   作者:晚亭聽鈴  
這篇文章主要介紹了Python利用imshow制作自定義漸變填充柱狀圖(colorbar),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

目的

在各種各樣的理論計(jì)算中,常常需要繪制各種填充圖,繪制完后需要加漸變填充的colorbar??墒怯行┸浖鏥MD,colorbar渲染后顏色分布有些失真,不能較準(zhǔn)確的表達(dá)各顏色對應(yīng)的數(shù)值。用ps中的漸變填充可以解決該問題,但很多電腦配置較低,不能很好的運(yùn)行ps。Python也可以直接繪制colorbar,填充顏色就好。如cmap中的bwr漸變本人就比較常用。然而,有時(shí)候顏色范圍是負(fù)數(shù)范圍多于正數(shù)范圍(如:colorbar需要表示 [-60,40]這段,藍(lán)色表示負(fù)數(shù),紅色表示正數(shù),白色應(yīng)該在colorbar由下往上60%處),bwr漸變將white置于50%處顯得不夠合理,因此需要自定義填充。本文以imshow() 函數(shù)來進(jìn)行填充柱狀圖達(dá)到自定義colorbar的目的。interpolation=‘bicubic' 可以很好的做出漸變效果。

代碼

# -*- coding: utf-8 -*-
"""
Created on Wed Dec 9 10:36:54 2020

@author: fya
"""

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap,LinearSegmentedColormap
import matplotlib as mpl

fig, ax = plt.subplots(dpi=96)
ax.set(xlim=(1,10), ylim=(-0.1,101), autoscale_on=False) #創(chuàng)建圖像范圍

a = np.array([[1, 1],
       [2, 2],
       [3, 3],
       [4, 4],
       [5, 5]]) #每種漸變色分成五段(array五行),數(shù)字表示在colormap對應(yīng)的深淺
print(a.shape)

clist=['white','blue'] #線性變化顏色由上面array值 小到大,越小,越白,達(dá)到上白下藍(lán)的漸變效果
clist2=['red','white'] #漸變色2,用于白色到紅色填充,array越小,越紅,達(dá)到上紅下白的效果
newcmp = LinearSegmentedColormap.from_list('chaos',clist)
newcmp2 = LinearSegmentedColormap.from_list('chaos',clist2)


plt.imshow(a,cmap=newcmp,interpolation='bicubic',extent=(1,10,0,60))#60%都是藍(lán)色到白色漸變
plt.imshow(a,cmap=newcmp2,interpolation='bicubic',extent=(1,10,60,100)) #白色設(shè)置在60%處

frame = plt.gca() #讀取當(dāng)前圖層
ax.yaxis.tick_right() #縱坐標(biāo)移到右邊
ax.set_yticklabels(('-80','-60','-40','-20','0','20','40')) #自定義yticks顯示的值,第一個(gè)label不顯示
frame.spines['top'].set_visible(False) #上框線不顯示
frame.spines['bottom'].set_visible(False)
frame.spines['right'].set_visible(False)
frame.spines['left'].set_visible(False)
plt.xticks([]) #x坐標(biāo)不要


plt.show()
fig.savefig('colorbar.tif',dpi=600,format='tif')
print('Done!')

#N = 10
#x = np.arange(N) + 0.15
#y = np.random.rand(N)

#width = 0.4
#for x, y in zip(x, y):
  #ax.imshow(a, interpolation='bicubic', extent=(x, x+width, 0, y), cmap=plt.cm.Blues_r)

#ax.set_aspect('auto')
#plt.show()

代碼2,漸變色分100段

# -*- coding: utf-8 -*-
"""
Created on Wed Dec 9 10:36:54 2020

@author: fanyiang
"""

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap,LinearSegmentedColormap
import matplotlib as mpl
import pandas as pd
import os

fig, ax = plt.subplots(dpi=96)
ax.set(xlim=(1,10), ylim=(-0.1,101), autoscale_on=False)

#a = np.array([[1, 1],
       #[2, 2],
       #[3, 3],
       #[4, 4],
       #[5, 5]]) #每種漸變色分成五段(array五行),數(shù)字表示在colormap對應(yīng)的深淺
avalue=locals() 
dfvalue=locals()      
for i in range(1,101):
  avalue['a'+str(i)]=np.array([[i,i]]) #漸變色分為100段,分的更細(xì)
  dfvalue['df'+str(i)]=pd.DataFrame(avalue['a'+str(i)]) #轉(zhuǎn)dataframe
  df=dfvalue['df'+str(i)]
  df.to_csv("temp.csv", mode='a',header=None) #暫存csv文件,第一列會把每一次循環(huán)的index放進(jìn)去
df3=pd.read_csv('temp.csv',header=None)#讀取csv
df3.columns=['序號','x','y']#column命名,第一列廢棄
df3=df3.drop('序號',axis=1)#刪除第一列
a=np.array(df3) #轉(zhuǎn)array
print(df3.head())

                                                                      
                                                                  
#a=np.vstack((a1,a2,a3,a4,a5,a6,a7,a8,a9,a10))

print(a)

clist=['white','blue'] #線性變化顏色由上面array值 小到大
clist2=['red','white']
newcmp = LinearSegmentedColormap.from_list('chaos',clist)
newcmp2 = LinearSegmentedColormap.from_list('chaos',clist2)


plt.imshow(a,cmap=newcmp,interpolation='bicubic',extent=(1,10,0,60))
plt.imshow(a,cmap=newcmp2,interpolation='bicubic',extent=(1,10,60,100)) #白色設(shè)置在60%處

frame = plt.gca() #讀取當(dāng)前圖層
ax.yaxis.tick_right() #縱坐標(biāo)移到右邊
ax.set_yticklabels(('-80','-60','-40','-20','0','20','40')) #自定義yticks顯示的值,第一個(gè)label不顯示
frame.spines['top'].set_visible(False) #上框線不顯示
frame.spines['bottom'].set_visible(False)
frame.spines['right'].set_visible(False)
frame.spines['left'].set_visible(False)
plt.xticks([]) #x坐標(biāo)不要


plt.show()
fig.savefig('colorbar.tif',dpi=600,format='tif')
os.remove("temp.csv") #刪除臨時(shí)的csv文件
print('Done!')

#N = 10
#x = np.arange(N) + 0.15
#y = np.random.rand(N)

#width = 0.4
#for x, y in zip(x, y):
  #ax.imshow(a, interpolation='bicubic', extent=(x, x+width, 0, y), cmap=plt.cm.Blues_r)

#ax.set_aspect('auto')
#plt.show()

效果

效果1

在這里插入圖片描述

效果2

在這里插入圖片描述

到此這篇關(guān)于Python利用imshow制作自定義漸變填充柱狀圖(colorbar)的文章就介紹到這了,更多相關(guān)Python 漸變填充柱狀圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

宁明县| 建平县| 富川| 邵阳市| 奉节县| 南开区| 林甸县| 余干县| 通江县| 灵璧县| 蒙自县| 甘德县| 孝昌县| 吉隆县| 昌江| 北辰区| 漳浦县| 崇文区| 尤溪县| 桂阳县| 永嘉县| 内乡县| 陵水| 凤城市| 定西市| 安龙县| 延川县| 库尔勒市| 凌源市| 嘉鱼县| 张家界市| 永和县| 兴山县| 阳江市| 昌乐县| 庆阳市| 抚州市| 威宁| 桑植县| 江永县| 出国|