Matplotlib - Global Legend and Subtitled Title

I started with matplot and managed some basic plots, but now it's hard for me to figure out how to do some of the things I need now :(

My question is how to put a global heading and a global legend on a figure with subtitles.

I make 2x3 subnets, where I have many different graphs in different colors (about 200). To distinguish (most) of them, I wrote something like

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 

(any thoughts on this? :)) Each subplot has the same style function.

Now I am trying to get a global heading for all the subplots, as well as a global legend that explains all the styles. I also need to make the font tiny so that it matches all 200 styles (I don't need completely unique styles, but at least some attempts)

Can someone help me solve this problem?

+52
matplotlib title legend subplot
Sep 23 2018-11-11T00:
source share
4 answers

Global header . In newer versions of matplotlib, you can use Figure.suptitle () .

 from pylab import * fig = gcf() fig.suptitle("Title centered above all subplots", fontsize=14) 
+102
Feb 10 2018-12-12T00:
source share

In addition to the orbeckst answer, you may also need to move the subheadings down. Here's the MWE in OOP style:

 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") 

gives:

enter image description here

+18
Aug 21 '15 at 10:25
source share

For legend labels, you can use something like below. Legendlabels are plot lines saved. modFreq is the name of the actual labels corresponding to the lines of the graph. Then the third parameter is the location of the legend. Finally, you can pass any arguments like I am here, but the first three are needed first. In addition, it is assumed that you set the labels correctly in the plot command. To simply call up the legend with the location parameter and find the labels in each of the lines. I was lucky that I made my legend as shown below. It seems that it works in all cases when it never seemed that he was doing the right thing. If you do not understand, let me know:

 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) 

You can also use your foot to change fonts or almost any legend parameter.

The global header specified in the above comment can be executed with the text added at the specified link: 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') 
+7
Sep 23 '11 at 12:26
source share

suptitle seems capable, but for what it's worth, figure has a transFigure property that you can use:

 fig=figure(1) text(0.5, 0.95, 'test', transform=fig.transFigure, horizontalalignment='center') 
+3
Oct 14
source share



All Articles