2013-08-16 25 views
21

我想要一個包含四個子圖的圖。其中兩個是通常的線圖,其中兩個是imshow圖像。多個imshow子圖,每個子圖都帶有colorbar

我可以將imshow-images格式化爲適當的圖表,因爲它們中的每一個都需要自己的顏色條,修改後的軸和另一個軸被移除。 然而,這似乎是絕對無用的subplotting。任何人都可以幫助我嗎?

我用它來顯示上面的「常規」圖的數據作爲色彩映射(通過將輸入數組i縮放爲[ i, i, i, i, i, i ]用於2D並使用它調用imshow())。

下面的代碼首先顯示我需要作爲一個子圖,第二個顯示我可以做的所有事情,這是不夠的。

#!/usr/bin/env python 

import matplotlib.pyplot as plt 
from matplotlib.colors import LogNorm 

s = { 't':1, 'x':[1,2,3,4,5,6,7,8], 'D':[0.3,0.5,0.2,0.3,0.5,0.5,0.3,0.4] } 
width = 40 

# how I do it in just one plot 
tot = [] 
for i in range(width): 
    tot.append(s['D']) 

plt.imshow(tot, norm=LogNorm(vmin=0.001, vmax=1)) 
plt.colorbar() 
plt.axes().axes.get_xaxis().set_visible(False) 
plt.yticks([0, 2, 4, 6], [s['x'][0], s['x'][2], s['x'][4], s['x'][6]]) 

plt.show() 


f = plt.figure(figsize=(20,20)) 

plt.subplot(211) 
plt.plot(s['x'], s['D']) 
plt.ylim([0, 1]) 

#colorplot 
sp = f.add_subplot(212) 

#reshape (just necessary to see something) 
tot = [] 
for i in range(width): 
    tot.append(s['D']) 

sp.imshow(tot, norm=LogNorm(vmin=0.001, vmax=1)) 

    #what I can't do now but needs to be done: 
    #sp.colorbar() 
#sp.axes().axes.get_xaxis().set_visible(False) 
#sp.yticks([0, 200, 400, 600, 800, 1000], [s['x'][0], s['x'][200], s['x'][400], s['x'][600], s['x'][800], s['x'][1000]]) 

plt.show() 
+0

__您的示例不運行!__您可以爲s和tot添加一些示例數據,以便我們可以看到您正在查看的內容嗎?爲了完整性,如果每個示例都以show命令結束,那也是很好的。 –

+0

對不起,我添加了可運行代碼。 – michael

+0

你不需要所有'cla'命令。此外,發佈示例時,最容易繪製隨機數據(除非問題取決於數據的確切值)。 – tacaswell

回答

40

您可以利用matplotlibs面向對象的接口而不是狀態機接口來更好地控制每個軸。另外,爲了控制顏色條的高度/寬度,您可以使用matplotlib的AxesGrid工具包。

例如:

import matplotlib.pyplot as plt 
import numpy as np 
from mpl_toolkits.axes_grid1 import make_axes_locatable 
from matplotlib.colors import LogNorm 
from matplotlib.ticker import MultipleLocator 

s = {'t': 1, 
    'x': [1, 2, 3, 4, 5, 6, 7, 8], 
    'T': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8], 
    'D': [0.3, 0.5, 0.2, 0.3, 0.5, 0.5, 0.3, 0.4]} 

width = 40 

tot = np.repeat(s['D'],width).reshape(len(s['D']), width) 
tot2 = np.repeat(s['T'],width).reshape(len(s['D']), width) 

fig, (ax1, ax2, ax3, ax4) = plt.subplots(1,4) 

fig.suptitle('Title of figure', fontsize=20) 

# Line plots 
ax1.set_title('Title of ax1') 
ax1.plot(s['x'], s['T']) 
ax1.set_ylim(0,1) 

ax2.set_title('Title of ax2') 
ax2.plot(s['x'], s['D']) 
# Set locations of ticks on y-axis (at every multiple of 0.25) 
ax2.yaxis.set_major_locator(MultipleLocator(0.25)) 
# Set locations of ticks on x-axis (at every multiple of 2) 
ax2.xaxis.set_major_locator(MultipleLocator(2)) 
ax2.set_ylim(0,1) 

ax3.set_title('Title of ax3') 
# Display image, `aspect='auto'` makes it fill the whole `axes` (ax3) 
im3 = ax3.imshow(tot, norm=LogNorm(vmin=0.001, vmax=1), aspect='auto') 
# Create divider for existing axes instance 
divider3 = make_axes_locatable(ax3) 
# Append axes to the right of ax3, with 20% width of ax3 
cax3 = divider3.append_axes("right", size="20%", pad=0.05) 
# Create colorbar in the appended axes 
# Tick locations can be set with the kwarg `ticks` 
# and the format of the ticklabels with kwarg `format` 
cbar3 = plt.colorbar(im3, cax=cax3, ticks=MultipleLocator(0.2), format="%.2f") 
# Remove xticks from ax3 
ax3.xaxis.set_visible(False) 
# Manually set ticklocations 
ax3.set_yticks([0.0, 2.5, 3.14, 4.0, 5.2, 7.0]) 

ax4.set_title('Title of ax4') 
im4 = ax4.imshow(tot2, norm=LogNorm(vmin=0.001, vmax=1), aspect='auto') 
divider4 = make_axes_locatable(ax4) 
cax4 = divider4.append_axes("right", size="20%", pad=0.05) 
cbar4 = plt.colorbar(im4, cax=cax4) 
ax4.xaxis.set_visible(False) 
# Manually set ticklabels (not ticklocations, they remain unchanged) 
ax4.set_yticklabels([0, 50, 30, 'foo', 'bar', 'baz']) 

plt.tight_layout() 
# Make space for title 
plt.subplots_adjust(top=0.85) 
plt.show() 

enter image description here


可以改變上或者與set_ticksset_ticklabels方法如在上面的例子軸刻度線的位置和標籤。


至於make_axes_locatable函數做什麼,從matplotlib site about the AxesGrid toolkit

的axes_divider模塊提供了一個輔助功能 make_axes_locatable,它可以是有用的。它需要一個現有的軸 實例併爲其創建一個分頻器。

ax = subplot(1,1,1) 
divider = make_axes_locatable(ax) 

make_axes_locatable返回AxesLocator類, 從定位器導出的一個實例。它提供了append_axes方法,該方法在原軸的(「top」,「right」,「bottom」和「left」) 的給定側創建一個新軸。

+1

http://stackoverflow.com/questions/14254379/how-can-i-attach-a-pyplot-function-to-a-figure-instance/14261698#14261698 < - 插入我的答案重新狀態機vs OO – tacaswell

+0

非常好,謝謝。 – michael

+0

make_axis_locatable exactely做什麼? – michael

相關問題