简体   繁体   中英

Matplotlib: Multiple legends on same figure

I have a few plot functions that each receive different kinds of data and plot them, with their own title and legends. However, when I try to plot them on the same figure, the former one is overwritten.

I understand that one way to do this is to call get_artist() , however since the legends are created inside functions, this doesn't seem possible. Is there any type of LegendHandler to do this? To retrieve the legend from each plot and show them on the figure? Should I return the legend from each function?

Here is a simplified code of what I have:

def plot_1(fig, data, ax = None):
    if ax = None:
        ax = fig.add_subplot(111)
    ax.plot(data)
    ax.set_xlabel('t')
    plt.axis('tight')

    # Create a legend
    colors = {'A':'blue',
              'B':'yellow',
              'C':'red'}
    legend_labels = []
    legend_handles = []

    for key in colors.keys():
        legend_labels.append(key)
        p = matplotlib.patches.Rectangle((0, 0), 1, 1, fc = colors[key], alpha = 0.5)
        legend_handles.append(p)
    ax.legend(legend_handles, legend_labels, loc='center left', bbox_to_anchor=(1, 0.5))

def plot_2(fig, data, ax = None):
    if ax = None:
        ax = fig.add_subplot(111)
    ax.plot(data, color='green', linewidth=1, label='L1')
    ax.set_xlabel('t')
    ax.autoscale()
    ax.legend()

def main():

    plot_1(fig, data = data1)
    plot_2(fig, data = data2)

I'm not sure if this is the best way to do it, but since I am using these plotting models multiple times, I feel that I have to keep them in function forms as they are.

Note from comment:

  • matplotlib: 2 different legends on same graph is the reason I mentioned get_artist , which was mentioned in that thread. I have actually solved it using that for now, but I was looking for a cleaner way for this particular case. What I have done for now is to return a legend from each function and do what was suggested in that thread.

Make your functions return the legend

def plot_2(fig, data, ax = None):
    ....
    return ax.legend()

Then you can do

legend1 = plot_1(fig, data = data1)
plot_2(fig, data = data2)
pyplot.gca().add_artist(legend1)

As suggested here: matplotlib: 2 different legends on same graph

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM