2015-06-10 140 views
5

我想繪製3個子圖,而它們之間沒有任何空白。默認的y軸標籤標籤使用顯示在y軸右上角的標度(下面例子中的1e-8),除了與上面的圖相重疊的較低的兩個標繪圖外,這將是很好的。有人知道怎麼修這個東西嗎?下面是一個小例子。Matplotlib子圖y軸縮放比例與上面的圖形重疊

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 
from matplotlib.ticker import MaxNLocator 

x = np.arange(0,200) 
y = np.random.rand(200) * 10e-8 


fig = plt.figure(figsize=(10,15)) 
gs1 = gridspec.GridSpec(3, 3) 
gs1.update(left=0.1, right=0.9, bottom=0.5, hspace=0.0) 
ax0a = plt.subplot(gs1[0, :]) 
ax0b = plt.subplot(gs1[1, :]) 
ax0c = plt.subplot(gs1[2, :]) 


ax0a.set_xticklabels([]) 
ax0b.set_xticklabels([]) 

ax0a.plot(x,y) 
nbins = len(ax0a.get_xticklabels()) 
ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) 
ax0b.plot(x,y) 
ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) 
ax0c.plot(x,y) 
ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) 

plot

這樣一個解決方案是使用mtick,

import matplotlib.ticker as mtick 

ax0a.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e')) 
ax0b.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e')) 
ax0c.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e')) 

,但我寧願能規模轉移到左邊,這樣它的軸線之外,如果可能的。

+0

你能否劃分你的數據通過'10e8',只需將xtick標籤重命名爲'r'$ 10^x $''? – GWW

回答

5

我有兩個選項,你可能想看看。

首先,設置軸位置和大小自己作爲這樣:

# your imports and data above 
fig = plt.figure() 
ax0a = fig.add_axes([0.1, 0.1, 0.8, 0.25]) 
ax0b = fig.add_axes([0.1, 0.39, 0.8, 0.25], sharex=ax0a) 
ax0c = fig.add_axes([0.1, 0.68, 0.8, 0.25], sharex=ax0a) 
ax0a.set_xticklabels([]) 
ax0b.set_xticklabels([]) 
ax0a.plot(x,y) 
nbins = len(ax0a.get_xticklabels()) 
ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) 
ax0b.plot(x,y) 
ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) 
ax0c.plot(x,y) 
ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) 
plt.show() 

enter image description here

第二個選擇是手動調整偏移文本的位置和可能的字體大小:

# your original code minus data and imports 
fig = plt.figure() 
gs1 = gridspec.GridSpec(3, 3) 
gs1.update(left=0.1, right=0.9, bottom=0.5, hspace=0.0) 
ax0a = plt.subplot(gs1[0, :]) 
ax0b = plt.subplot(gs1[1, :]) 
ax0c = plt.subplot(gs1[2, :]) 
ax0a.set_xticklabels([]) 
ax0b.set_xticklabels([]) 
ax0a.plot(x,y) 
nbins = len(ax0a.get_xticklabels()) 
ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) 
ax0b.plot(x,y) 
ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) 
ax0c.plot(x,y) 
ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) 

# play around with location and font of offset text here 
ax0a.get_yaxis().get_offset_text().set_x(-0.075) 
ax0a.get_yaxis().get_offset_text().set_size(10) 
ax0b.get_yaxis().get_offset_text().set_x(-0.075) 
ax0b.get_yaxis().get_offset_text().set_size(10) 
ax0c.get_yaxis().get_offset_text().set_x(-0.075) 
ax0c.get_yaxis().get_offset_text().set_size(10) 
plt.show() 

enter image description here

+1

太棒了!第二個例子正是我需要的。 – Dave

0

好吧,這可能是一個醜陋的解決方案,但您可以簡單地升級您的數據在較低的地塊 與權力的範圍,即ax0b.plot(x, y*1e8)。 至少適用於您的示例。

+0

不,我不能這樣做,我需要保持它已經在它的單位,但謝謝你的建議。 – Dave

相關問題