17

I want to create a plot with a function, it will return a fig so later on I can redisplay it when needed.

The function goes like this:

def simple_plot(ax = None):
    if ax is None:
        fig, ax = plt.subplots()
    a = [1,2,3,4]
    b = [3,4,5,6]
    plt.plot(a, b,'-', color='black')
    return fig

If I run simple_plot(), it would print the plot twice, like this:

enter image description here

Notice: if I run fig = simple_plot(), it will only print once, and I can use fig to reproduce the plot later in Ipython Notebook

  1. How can I make it only print once if I run simple_plot()?

  2. I'm not sure if I defined the function correctly, what would be a good way to define a function to make plot?

2
  • Your problem isn't very clear, could you have a read through and try to fix some of the issues? Commented Feb 16, 2016 at 3:21
  • @Jezzamon, I have update my question, please see it again, thanks. Commented Feb 16, 2016 at 3:29

4 Answers 4

23

This is a side effect of the automatic display functionality of the Jupyter Notebooks. Whenever you call plt.plot() it triggers the display of the plot. But also, Jupyter displays the return value of the last line of every cell, so if the figure object is referenced as the last statement of the cell, another display is triggered. If the last statement of the cell is an assignment (fig = simple_plot()), the return value is None and thus a second display is not triggered and you don't get the second plot.

Sign up to request clarification or add additional context in comments.

2 Comments

This seems very inconvenient for people writing packages that produce custom plots. Is there no way to write a function that returns a plot but doesn't require unusual user behavior to avoid having the plot duplicated?
Excellent answer. Just wanted to add that it works fine without adding the semi colon as well. Simply just doing an assignation to object 'fig' in your last line of the cell as @foglerit has stated.
14

Just add plt.close() before return, like this:

def simple_plot(ax = None):
    if ax is None:
        fig, ax = plt.subplots()
    a = [1,2,3,4]
    b = [3,4,5,6]
    plt.plot(a, b,'-', color='black')
    plt.close()
    return fig

Comments

1

Remove your return statement

Ipython Notebook is plotting once when you execute plt.plot(a, b,'-', color='black') and a second time when you return your fig object to the console.

You could as well keep the return statement, but store the returned value into a variable and plot the figure again just by executing fig.

enter image description here

2 Comments

This means I can't run simple_plot()? The variable assignment fig = simple_plot() is quite unnecessary when I don't need the fig.
You can but then you don't want a return statement because that will cause the double plot (or you could add the semicolon like jonnat's answer suggested)
1

I also encountered this thing. Indeed this is a side effect of the automatic rendering engine embedded in the Jupyter Notebook.

I've found this simple hack. Just use ; after the plot()

for e.g. :

plt.rc("figure",figsize=(8,8))
seasonal_decompose(df,model='additive',period=4).plot();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.