Issue
Here is my Python code to extract segments from a Venn Diagrams:
import matplotlib.pyplot as plt
from matplotlib_venn import venn2
set1_size = 20
set2_size = 25
intersection_size = 5
fig, axs = plt.subplots(2, 1, figsize=(6, 6))
v1 = venn2(subsets=(set1_size, set2_size, intersection_size), ax=axs[0])
v1.get_patch_by_id('11').set_color('purple')
v1.get_patch_by_id('10').set_alpha(0)
v1.get_patch_by_id('01').set_alpha(0)
v1.get_label_by_id('10').set_text('')
v1.get_label_by_id('01').set_text('')
v1.get_label_by_id('11').set_text('')
v2 = venn2(subsets=(set1_size, set2_size, intersection_size), ax=axs[1])
v2.get_patch_by_id('11').set_color('purple')
v2.get_patch_by_id('10').set_color('#0000FF') # Hex code for blue
v2.get_patch_by_id('01').set_alpha(0)
v2.get_label_by_id('10').set_text('')
v2.get_label_by_id('01').set_text('')
v2.get_label_by_id('11').set_text('')
axs[0].set_xticks([])
axs[0].set_yticks([])
axs[1].set_xticks([])
axs[1].set_yticks([])
plt.axhline(y=0.5, color='black', linestyle='-')
plt.subplots_adjust(hspace=0.5)
plt.show()
I want to center everything properly - this represents the division:
I looked at different ways to see if this is possible in Python, but so far nothing makes sense. Is there some way in Python to align the visualizations? Right now I am doing by hand. Is there some default option/code that can be written for this?
PS: Here is code for original venn diagram
import matplotlib.pyplot as plt
from matplotlib_venn import venn2
set_size = 20
intersection_size = 5
v = venn2(subsets=(set_size, set_size, intersection_size),
set_labels=('A', 'B'))
v.get_patch_by_id('11').set_color('purple')
v.get_patch_by_id('10').set_color('#FF0000')
v.get_patch_by_id('01').set_color('#0000FF')
v.get_label_by_id('10').set_text('')
v.get_label_by_id('01').set_text('')
v.get_label_by_id('11').set_text('')
plt.show()
Solution
You could try to use matplotlib.pyplot.subplot_mosaic to "pad" your Venn Diagrams with empty subplots in such a way that the figure looks closer to what you want, e.g. like this:
import matplotlib.pyplot as plt
from matplotlib_venn import venn2
set1_size = 20
set2_size = 25
intersection_size = 5
subplot_kw = {'figsize': (6, 6)}
fig, axs = plt.subplot_mosaic(
[
['e1', 'v1', 'v1', 'v1', 'v1'],
['e1', 'e2', 'v2', 'v2', 'v2']
],
layout='constrained',
**subplot_kw
)
v1 = venn2(subsets=(set1_size, set2_size, intersection_size), set_labels=('', ''), ax=axs['v1'])
v1.get_patch_by_id('11').set_color('purple')
v1.get_patch_by_id('10').set_alpha(0)
v1.get_patch_by_id('01').set_alpha(0)
v1.get_label_by_id('10').set_text('')
v1.get_label_by_id('01').set_text('')
v1.get_label_by_id('11').set_text('P(AnB)')
v2 = venn2(subsets=(set1_size, set2_size, intersection_size), set_labels=('', ''), ax=axs['v2'])
v2.get_patch_by_id('11').set_color('purple')
v2.get_patch_by_id('10').set_color('#0000FF') # Hex code for blue
v2.get_patch_by_id('01').set_alpha(0)
v2.get_label_by_id('10').set_text('P(B)')
v2.get_label_by_id('01').set_text('')
v2.get_label_by_id('11').set_text('')
axs['v1'].set_axis_off()
axs['v2'].set_axis_off()
axs['e1'].set_axis_off()
axs['e2'].set_axis_off()
axs['e1'].text(1, 0.4, 'P(A|B) = ')
plt.axhline(y=0.5, xmin=0, xmax=0.6, color='black', linestyle='-')
plt.show()
This is what I got as a result:
You may need to tune the number of padding subplots and their placement as well. The layout changes if you resize the window. Surely it is not an optimal solution, but at least it works to some extent.
Answered By - Ratislaus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.