Quantcast
Viewing latest article 9
Browse Latest Browse All 11

Answer by carla for How do I make a single legend for many subplots?

You just have to ask for the legend once, outside of your loop.

For example, in this case I have 4 subplots, with the same lines, and a single legend.

from matplotlib.pyplot import *ficheiros = ['120318.nc', '120319.nc', '120320.nc', '120321.nc']fig = figure()fig.suptitle('concentration profile analysis')for a in range(len(ficheiros)):    # dados is here defined    level = dados.variables['level'][:]    ax = fig.add_subplot(2,2,a+1)    xticks(range(8), ['0h','3h','6h','9h','12h','15h','18h','21h'])     ax.set_xlabel('time (hours)')    ax.set_ylabel('CONC ($\mu g. m^{-3}$)')    for index in range(len(level)):        conc = dados.variables['CONC'][4:12,index] * 1e9        ax.plot(conc,label=str(level[index])+'m')    dados.close()ax.legend(bbox_to_anchor=(1.05, 0), loc='lower left', borderaxespad=0.)         # it will place the legend on the outer right-hand side of the last axesshow()

Viewing latest article 9
Browse Latest Browse All 11

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>