2015-06-10 303 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

相关问题