2011-09-23 25 views
60

我已经开始与matplot和管理的一些基本情节,但现在我觉得很难发现如何做一些东西,我现在:(Matplotlib - 全球传说和标题一旁的次要情节

我实际的问题是如何需要将一个全局标题和全局图例放在一个带有子图的图上

我正在做2x3子图,其中有很多不同颜色的图形(大约200)。像

def style(i, total): 
    return dict(color=jet(i/total), 
       linestyle=["-", "--", "-.", ":"][i%4], 
       marker=["+", "*", "1", "2", "3", "4", "s"][i%7]) 

fig=plt.figure() 
p0=fig.add_subplot(321) 
for i, y in enumerate(data): 
    p0.plot(x, trans0(y), "-", label=i, **style(i, total)) 
# and more subplots with other transN functions 

(对此有何看法?:))每个子图具有相同的风格功能。

现在我试图让所有的小插图的全球标题,也是一个解释所有风格的全球传奇。此外,我需要使字体很小,以适应所有200种风格(我不需要完全独特的风格,但至少有一些尝试)

有人可以帮我解决这个任务吗?

+1

全球标题:http://matplotlib.sourceforge.net/examples/pylab_examples/newscalarformatter_demo.html –

回答

112

全球标题:在matplotlib的新版本可以使用Figure.suptitle()

from pylab import * 
fig = gcf() 
fig.suptitle("Title centered above all subplots", fontsize=14) 
+33

对于那些进口这样的:'import matplotlib.pyplot as plt',该命令可以简单地输入为'plt.figure(); plt.suptitle('标题集中在所有子图上面'); plt.subplot(231); plt.plot(data [:,0],data [:,1]);'etc ... –

+0

谢谢。这应该是实际选定的答案。 – gustafbstrom

7

对于图例标签可以使用类似下面的内容。图例标签是保存的绘图线。 modFreq是绘制线对应的实际标签的名称。然后第三个参数是图例的位置。最后,你可以传入任何参数,但我主要需要前三个参数。另外,如果您在绘图命令中正确设置了标签,则应该这样做。只需使用location参数调用图例,并在每行中找到标签。我有更好的运气,使自己的传奇如下。似乎工作在所有情况下,似乎没有得到正确的其他方式。如果您不明白,请告诉我:

legendLabels = [] 
for i in range(modSize): 
    legendLabels.append(ax.plot(x,hstack((array([0]),actSum[j,semi,i,semi])), color=plotColor[i%8], dashes=dashes[i%4])[0]) #linestyle=dashs[i%4]  
legArgs = dict(title='AM Templates (Hz)',bbox_to_anchor=[.4,1.05],borderpad=0.1,labelspacing=0,handlelength=1.8,handletextpad=0.05,frameon=False,ncol=4, columnspacing=0.02) #ncol,numpoints,columnspacing,title,bbox_transform,prop 
leg = ax.legend(tuple(legendLabels),tuple(modFreq),'upper center',**legArgs) 
leg.get_title().set_fontsize(tick_size) 

您还可以使用leg来更改字体大小或几乎图例的任何参数。如上面的评论说

全球标题可以按照提供的链接添加文字来完成: http://matplotlib.sourceforge.net/examples/pylab_examples/newscalarformatter_demo.html

f.text(0.5,0.975,'The new formatter, default settings',horizontalalignment='center', 
     verticalalignment='top') 
3

suptitle似乎走的路,但是这是非常值得的figuretransFigure属性,您可以使用:

fig=figure(1) 
text(0.5, 0.95, 'test', transform=fig.transFigure, horizontalalignment='center') 
20

除了orbeckst answer一个也可能要转移子情节下跌。下面是OOP风格的MWE:

import matplotlib.pyplot as plt 

fig = plt.figure() 
st = fig.suptitle("suptitle", fontsize="x-large") 

ax1 = fig.add_subplot(311) 
ax1.plot([1,2,3]) 
ax1.set_title("ax1") 

ax2 = fig.add_subplot(312) 
ax2.plot([1,2,3]) 
ax2.set_title("ax2") 

ax3 = fig.add_subplot(313) 
ax3.plot([1,2,3]) 
ax3.set_title("ax3") 

fig.tight_layout() 

# shift subplots down: 
st.set_y(0.95) 
fig.subplots_adjust(top=0.85) 

fig.savefig("test.png") 

给出:

enter image description here