i plotting grid in matplotlib. have 3*2 subplot grid this:

i plotting line charts in every subplot , each color of line specifies category. let's color_name = { cat1 : red , cat2 : black , ...} now, meaning of color same in every subplot may contain subset of color_name dict categories. need common legend plot contain color - category pair dict color_names. have searched lot not getting method make legend directly using dict contain each key-value pair dict once. here x-axis contain dates , y-value every day.
code:
def graph_data(data): ''' return html graph 1 department ''' fig, axes = plt.subplots( nrows=3, ncols=2, sharex = true, figsize=(14, 10), facecolor = 'w' , ) rownames = sorted(data.keys()) beautify(fig, axes, rownames ) plt.close() color_name = get_color_name_dict() i, metric in enumerate(sorted(data.keys())): j, device in enumerate(sorted(data[metric].keys())): ax = axes[i , j] ylow = 10000 yhigh = 0 page in data[metric][device].keys(): dd = data[metric][device][page] x = dd.keys() y = [statify(dd[_date]) _date in x] ylow = min(ylow , min(y)) yhigh = max(yhigh, max(y)) ax.xaxis.set_major_formatter(mdates.dateformatter('%d-%m-%y')) ax.xaxis.set_major_locator(mdates.daylocator()) ax.plot(y , color = color_name[page], label = page , linewidth = '2') ax.fmt_xdata = mdates.dateformatter('%d-%m-%y') tick in ax.get_xticklabels(): tick.set_rotation(45) fig.autofmt_xdate() ax.set_ylim([ylow-200 , yhigh+200]) fig.tight_layout() # tight_layout doesn't take these labels account. we'll need # make room. these numbers are manually tweaked. fig.subplots_adjust(left=0.18, top=0.95, right=0.95, bottom = 0.05) imgdata = cstringio.stringio() fig.savefig(imgdata, format='png' , facecolor = fig.get_facecolor()) s = '<img alt = "embedded" src = "data:image/png;base64,%s"/>' % imgdata.getvalue().encode("base64").strip() orig_stdout = sys.stdout f = open("test.html" , "w") sys.stdout = f print s sys.stdout = orig_stdout f.close() print "done" mpld3.show(fig) #return s beautify function:
def beautify(fig, axes, rows): ''' beautify plot ''' style.use('ggplot') cols = ['desktop' , 'mobile'] ax in axes[:,0]: ax.set_ylabel("median (ms)") pad = 5 # in points ax, col_name in zip(axes[0], cols): ax.annotate(col_name, xy=(0.5, 1), xytext=(0, pad), xycoords='axes fraction', textcoords='offset points', size='large', ha='center', va='baseline') ax, row_name in zip(axes[:,0], rows): ax.annotate(row_name, xy=(0, 0.5), xytext=(-ax.yaxis.labelpad - pad, 0), xycoords=ax.yaxis.label, textcoords='offset points', size='large', ha='right', va='center')