ZK Zhao
ZK Zhao

Reputation: 21523

Matplotlib: how to show plot again?

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.

Upvotes: 0

Views: 3337

Answers (1)

fernandezcuesta
fernandezcuesta

Reputation: 2448

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.

Upvotes: 1

Related Questions