Search code examples
pythonmatplotlibplotjupytermatplotlib-venn

Plotting Venn diagram in Jupyter after changes (matplotlib-venn)


By following the commands in the matplotlib-venn README I can produce the initial plots in the examples. However, when I change the settings of the Venn diagram (label text etc.) I cannot work out how to replot the figure. Running:

%matplotlib inline
from matplotlib_venn import venn3
v = venn3([set1, set2, set3], ('Set1', 'Set2', 'Set3'))

gives the Venn diagram inline. I then proceed to change a label

v.get_label_by_id('100').set_text('Arbitrary1')

but I cannot then replot the figure. I have tried

# from matplotlib import pyplot as plt
plt.plot()
plt.plot(v)
v
v()

but I am really feeling around in the dark. I feel like I am missing something very basic about %matplotlib or the matplotlib plot function, but I have not been able to find an answer yet online.

How do I plot this figure again in Jupyter?


Solution

  • If you use fig=plt.figure() to store a reference to figure instance, then you will have access to the figure in future notebook cells. If you don't do this, then you won't be able to access the existing figure in new cells.

    So, after you set the label, you would simply need to write fig again afterwards to show the figure again.

    Here's a working example:

    Cell 1:

    %matplotlib inline
    from matplotlib_venn import venn3
    import matplotlib.pyplot as plt
    fig = plt.figure()
    set1 = set(['A', 'B', 'C'])
    set2 = set(['A', 'D', 'E'])
    set3 = set(['A', 'F', 'B'])
    
    v = venn3([set1, set2, set3], ('Set1', 'Set2', 'Set3'))
    

    Cell 2:

    v.get_label_by_id('100').set_text('Arbitrary1')
    fig
    

    enter image description here