matplotlib - How to reverse the order of a legend when using stackplot in python? -
i have stackplot using python, , legend @ right show correct order of colours - i.e need reverse it. however, when reverse it, reverses colours, meaning colours in plot no longer correspond colours in legend.
the code have follows;
import matplotlib.colors colors import matplotlib.cm mplcm mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes mpl_toolkits.axes_grid1.inset_locator import mark_inset # zoom plot start , end start = 408 end = 518 num_colors = 10 cm = plt.get_cmap('gist_ncar') cnorm = colors.normalize(vmin=0, vmax=num_colors-1) scalarmap = mplcm.scalarmappable(norm=cnorm, cmap=cm) fig = plt.figure() ax = fig.add_subplot(111) # old way: ax.set_prop_cycle(color=[cm(1.*i/num_colors) in range(num_colors)]) # new way: # ax.set_prop_cycle([scalarmap.to_rgba(i) in range(num_colors)]) ax.stackplot(range(0, 672), gen_type_summary[0,range1:range2], baseline="zero", linewidth=0.1) ax.stackplot(range(0 , 672), gen_type_summary[1:12,range1:range2], baseline="zero", linewidth=0.1) plt.axis([0,672, -5000,95000]) plt.legend(gen_label[0:11], loc='center left', bbox_to_anchor=(1, 0.5)) plt.xlabel('hours') plt.ylabel('dispatch level (mw)') fig.dpi = 1600 fig.set_size_inches(w=16,h=5)
unfortunately stackoverflow image uploader isn't working showcase image. appreciated!
Comments
Post a Comment