5

With the following code I tried to create the Venn diagram then saved as a file.

import matplotlib
from matplotlib_venn import venn2
set1 = set(['A', 'B', 'C', 'D'])
set2 = set(['B', 'C', 'D', 'E'])
plt = venn2([set1,set2],('Set1','Set2'))
plt.savefig('test.png')

But it gave me error. What's the right way to do it?

This is the example figure where I exceuted under Ipython: enter image description here

Nabla
  • 1,509
  • 3
  • 20
  • 35
pdubois
  • 7,640
  • 21
  • 70
  • 99
  • This solution wasn't working for me, I'd only get an empty png. Then I found this other question [link](https://stackoverflow.com/questions/66289004/how-to-save-venndiagram-as-png-file-in-matplotlib-venn), which explained what I was getting wrong. So, if anyone here has the same problem as I do, here it is. – Maria Jan 30 '22 at 15:32

1 Answers1

7

venn2 is a function that returns an instance of VennDiagram. However, the class VennDiagram does not have the method savefig as you wish. What you are trying to do is to save the resulting figure. For that, based on your loaded modules, you can use the following command.

matplotlib.pyplot.savefig('test.png')

instead of

plt.savefig('test.png')

This might solve the problem for you.

Cheers

enter image description here

mrcl
  • 2,130
  • 14
  • 29