How to add plot commands to a figure in more than one cell, but display it only in the end?

后端 未结 1 1949
清酒与你
清酒与你 2020-12-20 09:27

I want to do the following in a Jupyter Notebook:

  • Create a pyplot.figure in a cell;
  • For each subsequent cells, calculate values and plot
相关标签:
1条回答
  • 2020-12-20 10:13

    You can capture the content of a cell of a jupyter notebook using the magic command %%capture. You can also hide any output of a specific line by putting a ; at the end of it.

    Showing the figure can be done by simply typing the variable in which the figure is stored, e.g. fig.

    Combining those techniques gives you

    import matplotlib.pyplot as plt
    %matplotlib notebook
    
    %%capture captured
    fig, ax=plt.subplots()
    
    ax.plot([1,2,3]);
    
    fig  # now show the figure
    

    which is probably more understandable in the acutal notebook like this:

    Also see How to overlay plots from different cells?

    0 讨论(0)
提交回复
热议问题