Matplotlib – 全球传奇和标题旁边的subplots

我已经开始用matplot和pipe理一些基本情节,但现在我发现很难发现如何做一些我现在需要的东西:(

我真正的问题是如何把一个全球性的标题和全球的传奇人物放在一个图上。

我正在做2×3子图,其中有许多不同颜色的图(大约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种风格(我不需要完全独特的风格,但至less有一些尝试)

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

全局标题 :在较新版本的matplotlib中,可以使用Figure.suptitle() 。

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

除了最好的答案之外,人们也可能想要将子图向下移动。 这是一个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") 

得到:

在这里输入图像说明

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

 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) 

您还可以使用该脚来更改字体大小或图例的几乎任何参数。

以上评论中所述的全局标题可以通过根据提供的链接添加文本来完成: 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') 

suptitle似乎是要走的路,但为了什么是值得的,这个figure有一个transFigure属性,你可以使用:

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