1

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?

Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
Will Bryant
  • 521
  • 5
  • 17

2 Answers2

8

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

Community
  • 1
  • 1
tmdavison
  • 64,360
  • 12
  • 187
  • 165
1

The state of the pyplot statemachine gets lost after the jupyter cell is evaluated. Thus in a new cell pyplot is unaware of the previously created figure.

The solution is to obtain a reference to the figure before leaving the cell. This is either done by

  • explicitely assigning the figure to a variable fig=plt.figure() or
  • storing the figure in a variable at the end of a cell, fig = plt.gcf()

In both cases you will have a figure object to show in later cells, by simply typing fig. Since this is general to using matplotlib in jupyter notebook, the following example does not take venn diagrams into account.

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712