How to create a figure legend in matplotlib with just one line the plotting subplots?

  Kiến thức lập trình

I have this code to plot 4 sub bar plots:

    def plot(self, concat_series, save_plot):
        grouped = concat_series.groupby("metric")

        fig, axes = plt.subplots(nrows=1, ncols=len(grouped))
        i=0
        for name, group in grouped:
            group.plot.bar(ax=axes[i], x='network', legend=False)
            axes[i].set_title(name)

            axes[i].grid(True)  # Add grid lines (optional)
            i+=1

        plt.tight_layout()  # Adjust spacing (optional)
        legend = fig.legend(loc="lower center", ncol=4, title="Coordinates")
        plt.savefig(save_plot)
        plt.close()

What gives me this:

As the for subplots have same legend notations, I would like to plot the legend with just one line instead of 4 repeated lines. How can I achieve this?

LEAVE A COMMENT