0

Let say I make a plot in Ipython Notebook, and after couples of cells, I want to render it again, so I can compare it with other plots.

How can I do it?

a = [1,2,3,4]
b = [3,4,5,6]
fig = plt.plot(a, b,'-', color='black')

This would display the plot, but when I run fig, there is no plot output.

I find this: matplotlib show figure again, but that seems pretty complex?

UPDATE: This is what I end up with:

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

fig = simple_plot() # This would plot
fig # this would also plot

However, if I run simple_plot(), it would print twice.

Community
  • 1
  • 1
ZK Zhao
  • 19,885
  • 47
  • 132
  • 206
  • you just have to catch the return value of `simple_plot()` (like you do when you have `fig = simple_plot()`), and then it won't plot twice. – tmdavison Feb 15 '16 at 09:52

1 Answers1

1

You can plot like this:

a = [1,2,3,4]
b = [3,4,5,6]
f = plt.figure()
f.add_subplot(111).plot(a, b,'-', color='black')

Then render again just by calling f.

fernandezcuesta
  • 2,390
  • 1
  • 15
  • 32
  • Additionally, if I want to make a `function` to plot, what should I `return`? So I can save the fig later for display. – ZK Zhao Feb 15 '16 at 08:52
  • @cqcn1991 that's another question. please search stackoverflow first, it's already answered. – Fabian Rost Feb 15 '16 at 09:08
  • You may pass `f` to the function or the AxesSubplot (`ax = f.add_subplot(111)`), then plot on it. – fernandezcuesta Feb 15 '16 at 09:13
  • @cqcn1991 isn't this what you looked for? I mean, showing the plot again? If you want to get the plot and not showing it in the IP notebook, you can just supress the cell output with `%%capture` – fernandezcuesta Feb 15 '16 at 10:00
  • @valtuarte yes, it almost solved my problem. But when I run `simple_plot()`, it will produce the plots twice. Can I make it just plot 1 time when I run `simple_plot()`? – ZK Zhao Feb 15 '16 at 12:39
  • well actually this will plot it once, then show it again since that wasn't assigned to any variable. If you assign it i.e. `saved_fig = simple_plot()` it only shows once, then you can redraw in the future. – fernandezcuesta Feb 15 '16 at 13:27