[英]Matplotlib Colourmap from transparent
我是 Python 的新手,我真的很難為我的數據獲取合理的顏色圖。
我正在繪制 29x29 numpy arrays,其中大多數單元格為 0,但平均大約 10-15 個單元格具有非零值,范圍從低 10s 到幾個 1000s。
在 C++ ROOT 中,您會自動獲得一個漂亮的 plot,它具有白色背景和漂亮的彩虹色條,您可以在下面看到。
但是,在 matplotlib 中,請遵循此處的建議:
python matplotlib 來自透明的熱圖顏色條使用代碼:
from matplotlib.colors import LinearSegmentedColormap
%matplotlib inline
#Lets visualise some events
# plot states
# plot states
# get colormap
ncolors = 256
color_array = plt.get_cmap('gist_rainbow')(range(ncolors))
# change alpha values
color_array[:,-1] = np.linspace(1.0,0.0,ncolors)
# create a colormap object
map_object = LinearSegmentedColormap.from_list(name='rainbow_alpha',colors=color_array)
# register this new colormap with matplotlib
plt.register_cmap(cmap=map_object)
# set colourbar map
cmap_args=dict(cmap='jet')
fig, axarr = plt.subplots(nrows=1, ncols=3)
axarr[0].imshow(events[0],**cmap_args)
axarr[0].set_title('Event0',fontsize=16)
axarr[0].tick_params(labelsize=16)
axarr[1].imshow(events[1],**cmap_args)
axarr[1].set_title('Event1',fontsize=16)
axarr[1].tick_params(labelsize=16)
axarr[2].imshow(events[2],**cmap_args)
axarr[2].set_title('Event2',fontsize=16)
axarr[2].tick_params(labelsize=16)
fig.subplots_adjust(right=2.0)
plt.show()
我得到像下面這樣的圖像,這是無法閱讀的。
請有人解釋一下如何在 plot 側面獲得白色背景和彩虹色條?
非常感謝!
要將所有零值顯示為白色,可以設置“下”顏色。 底色用於低於顏色欄中最低值的值。 使用vmin=1
強制顏色條從 1 開始,使所有低於 1 的值都被視為“低於”。
from matplotlib import pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator
#%matplotlib inline
# create a colormap object
cmap = plt.get_cmap('rainbow')
cmap.set_under('white')
# set colourbar map
cmap_args = dict(cmap=cmap, vmin=1, vmax=8000)
fig, axarr = plt.subplots(nrows=1, ncols=4, figsize=(12, 3), gridspec_kw={'width_ratios': [10, 10, 10, 1]})
events = np.random.randint(0, 9, size=(3, 10, 10)) * 1000 * np.random.randint(0, 2, size=(3, 10, 10))
for ax, event, title in zip(axarr[:3], events, ['Event 0', 'Event 1', 'Event 2']):
img = ax.imshow(event, **cmap_args)
ax.set_title(title, fontsize=16)
ax.tick_params(labelsize=16)
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.yaxis.set_major_locator(MultipleLocator(1))
fig.colorbar(img, cax=axarr[3])
# to make the colorbar exactly the same height as the image plots:
pos_ax2 = axarr[2].get_position()
pos_ax3 = axarr[3].get_position()
pos_ax3.y0 = pos_ax2.y0
pos_ax3.y1 = pos_ax2.y1
axarr[3].set_position(pos_ax3)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.