LankyCyril / pyvenn

Python module for plotting Venn diagrams of 2..6 sets
https://pypi.org/project/venn
GNU General Public License v3.0
120 stars 27 forks source link

How do I get the legend from the venn chart? #29

Closed 53845714nF closed 1 month ago

53845714nF commented 1 month ago

I use the following Code to get legend away from the venn chart:

sets = {
    'Trivy': trivy,
    'Grype': grype,
    'Snyk': snyk,
    'Docker Scout': scout,
    'Vesta': vesta,
    'Veinmind': veinmind
}

pseudovenn(sets,  hint_hidden=False, fontsize=14, legend_loc="best", ax=None)
plt.savefig('img/backend_all.png')
plt.clf()

But I get this: backend_all

The legend is on the venn chart.

LankyCyril commented 1 month ago

Hi, sorry for the slow reply.

Disclaimer: the library was published a long time ago when I knew a lot less about matplotlib. I need to find time to rewrite it to actually allow users to interact with the objects in a matplotlib-native way, but for now there's still ways around it.

I'd suggest telling venn not to plot the legend at all, and then to add the legend manually:

pseudovenn(sets,  hint_hidden=False, fontsize=14, legend_loc=None, ax=None)
plt.gca().legend(sets.keys(), loc="upper left", bbox_to_anchor=(1, 1))

This should anchor the upper left corner of the legend to the upper right corner (1, 1) of the axes, putting the legend outside of the diagram.

P.S. legend_loc="best" will unfortunately most often overlap with the diagram, because the axes are square and the diagram takes up most of the space in the square. Due to the old design, venn doesn't let you to specify bbox_to_anchor directly (and honestly it shouldn't...) but you could also simply try legend_loc="upper left", which by default would anchor to (0, 1), and see if that helps: pseudovenn(sets, hint_hidden=False, fontsize=14, legend_loc="upper left", ax=None)

53845714nF commented 1 month ago

Thank you for the quick reply, Ok had to indent the box a little. But it works now.

pseudovenn(sets, hint_hidden=False, fontsize=14, legend_loc=None, ax=None)
plt.gca().legend(sets.keys(), loc="upper left", bbox_to_anchor=(0.85, 0.9))
plt.savefig('img/backend_all.png')
plt.clf()

backend_all

Many thanks for your work