Sean Geoffrey Pietz
Sean Geoffrey Pietz

Reputation: 1140

when using matplotlib with ipython notebook plt.show() function doesn't work

I launched the ipython notebook using:

$ ipython notebook --pylab=inline 

in the command prompt.

when running this:

from matplotlib import pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot([1,2,3])

it automatically plots the graph without having to use the plt.show() function, however I find that the plt.show function doesn't work for me, or some other functions like plt.legend().

Does anybody know why this is happening, or how I can fix it?

Upvotes: 3

Views: 6840

Answers (2)

joaquin
joaquin

Reputation: 85603

As said, you do not need to import pyplot. Try:

fig = figure()
ax1 = fig.add_subplot(111)
ax1.plot([1,2,3])

Then:

ax1.legend(('hello',))
fig.show()

will plot the figure with a legend.
Using Ipython is very easy to look at the hints-docs and the available properties and methods.

Upvotes: 0

EnricoGiampieri
EnricoGiampieri

Reputation: 6085

The plt.show() doesn't do anything because when you activate the --pylab mode the matplotlib is executed in the interactive mode, which display even without using show().

About the legend, what do you mean it doesn't work? if you obtain an error like "No labeled objects found" it's because in the line code that you show you haven't set the label parameter to the plot

Upvotes: 2

Related Questions