2

I am following the code shown here to show the results of an experiment. However, I can't manage to show the labels, the interactivity works but still is not showing the labels. Here I send the labels to the plugin, but I don't know if I am missing some boolean parameter to allow the labels to be shown.

handles, labels = ax.get_legend_handles_labels() # return lines and labels
interactive_legend = plugins.InteractiveLegendPlugin(zip(handles, ax.collections),
                                                     labels,
                                                     alpha_unsel=0,
                                                     alpha_over=0, 
                                                     start_visible=True)
plugins.connect(fig, interactive_legend)

Any idea very appreciated it.

Demo

Vinícius Figueiredo
  • 6,300
  • 3
  • 25
  • 44

2 Answers2

4

The effect of labels not being shown with the InteractiveLegendPlugin is even present on the official example.

The reason may be better observed when changing the background color of the plot: The legend labels are cut off by the figure edge.

enter image description here

The solution is therefore to use a larger margin on the right side of the figure, such that the labels are within the figure edges. This can e.g. be done via

fig.subplots_adjust(right=0.7)

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
2

Finally I ended up using this code that creates a box in the bottom with the labels, making it look very organized:

plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fancybox=True, shadow=True, ncol=5)

Labels in a box