2016-08-30 84 views
0

我已经搜索了类似的问题,没有找到任何,所以我很抱歉。小区大小/滴答间距pyplot

我有这样的:

import matplotlib.pyplot as plt 

yearlymean_gm = np.load('ts_globalmean_annualmean.npz') 
ts = yearlymean_gm['ts_aqct'] 

time = np.arange(0., 45 , 1) 
plt.figure(figsize=(12, 5), dpi=80, facecolor='w', edgecolor='k') 

ax = plt.subplot(3, 4, 1) 

data = ts[0, :] 
plt.plot(time, data) 
plt.title('Annual Mean Global Mean Temperature', fontsize=14) 
plt.xlabel('year', fontsize=12) 
plt.ylabel(modnames[0], fontsize=12) 
plt.xlim(0, 50), plt.ylim(275, 310) 
ax.set_xticks(time) 
ax.set_xticklabels(time, fontsize = 8) 

ax= plt.subplot(3, 4, 2) 

data = ts[1, :] 
plt.plot(time, data) 
plt.title('Annual Mean Global Mean Temperature', fontsize=14) 
plt.xlabel('year', fontsize=12) 
plt.ylabel(modnames[1], fontsize=12) 
plt.xlim(0, 50), plt.ylim(275, 310) 
ax.set_xticks(time) 


ax = plt.subplot(3, 4, 3) 

data = ts[2, :] 
plt.plot(time, data) 
plt.title('Annual Mean Global Mean Temperature', fontsize=14) 
plt.xlabel('year', fontsize=12) 
plt.ylabel(modnames[2], fontsize=12) 
plt.xlim(0, 50), plt.ylim(275, 310) 
ax.set_xticks(time) 


plt.tight_layout() 
plt.show() 
plt.close 

目前有9个缺少情节,因为我敢肯定,你可以猜测,Here's what it's spitting out right now

这里有三个问题,是明确的: 1)每个subplot是tiny与图的大小相比较(并且,你知道...什么是容易看见的)。减小图的大小并不会使得子图更容易被读取。

2)它们太靠近了。我对如何解决这个问题有一些想法,但我觉得我需要先解决1)。

3)轴是如此之小,在xticks出现在所有揉成

我搜索,发现没有解释如何做到这一点,在真实的水平,我可以理解书面的。 pyplot文档对我来说基本上是一句胡言乱语。

在此先感谢您的帮助(如果有人可以就我正在做的事情提供更多的一般性建议以及有关解决此问题的具体建议,我将非常感激启蒙)。

回答

2

好吧,这里发生了几件事情。让我们一次一个地通过它们。在你实例化绘图之后,你可以拨打ax = plt.subplot(3, 4, _)三次。但是,.subplot(3,4,_)将绘图分解为3行和4列,并且下划线选择从1开始(而不是0)的该网格的哪一部分。我们可以用下面的代码数得过来吗

plt.figure(figsize=(12, 5), dpi=80, facecolor='w', edgecolor='k') 
for N in range(1,13): 
    ax = plt.subplot(3, 4, N) 
    ax.annotate(s=str(N), xy=(.4,.4), fontsize=16) 

enter image description here

所以但使用.subplot(3,4,1).subplot(3,4,2).subplot(3,4,3)您只选择了12个部分的前3。

当您将数据添加到图中时,ax.set_xticks(time)将xaxis添加了45个滴答声(这是很多),并且ax.set_xticklabels(time, fontsize = 8)在每个滴答滴答添加了一个标签。这就是为什么它看起来如此拥挤。一种选择是减少滴答声的数量,另一种是将x轴拉伸出来。既然你有3个子图,我想你想要3排垂直堆放。您可能并不需要plt.xlim(0, 50)plt.ylim(275, 310)。除非您有重写它们的特定原因,否则轴将为您调整绘图极限。

我的建议是使用plt.subplots(3,1)(注意额外的“s”),而不是重复调用plt.subplot。有什么区别? plt.subplots(3,1)返回对象的一个​​元组和一个axis对象的数组。在这种情况下,它是一维数组,因为我们只需要1列。 (注:我创建假数据用于说明的目的。)

fig, axes = plt.subplots(3,1, figsize=(12,5), dpi=80, facecolor='w', 
         edgecolor='k', sharex=True) # sharex shares the x-axis 

for i, ax in enumerate(axes): # need to enumerate to slice the data 
    data = ts[i, :] 
    ax.plot(time, data) 
    ax.set_ylabel(modnames[i], fontsize=12) 
    ax.set_xticks(time) 
    ax.set_xticklabels(time, fontsize = 8) 

# set xlabel outside of for loop so only the last axis get an xlabel 
ax.set_xlabel('year', fontsize=12) 
fig.tight_layout() 

# set the title, adjust the spacing 
fig.suptitle('Annual Mean Global Mean Temperature', fontsize=14) 
fig.subplots_adjust(top=0.90) 

enter image description here