Error displaying a matplotlib in ipython notebook
<p>I'm going though a ipython notebook tutorial and it says to run this in a cell.
    import numpy as np
    import math
    import matplotlib.pyplot as plt</p>

<pre><code>x = np.linspace(0, 2*math.pi) 
plt.plot(x, np.sin(x), label=r'$\sin(x)$') 
plt.plot(x, np.cos(x), 'ro', label=r'$\cos(x)$') 
plt.title(r'Two plots in a graph') 
plt.legend() 
</code></pre>

<p>and I should get an actual graph. Isntead I get</p>

<pre><code>&lt;matplotlib.legend.Legend at 0x1124a2fd0&gt;
</code></pre>

<p>What should I do instead?</p>

<p>Try to add this statement earlier in your notebook, which indicates to matplotlib where to render the plot (i.e. as an html element embedded in the notebook):</p>

<p><code>
%matplotlib inline
</code></p>

<p>The story behind that is simply that matplotlib is old enough to be existing since before jupyter and ipython notebooks became popular. Back then the standard way of creating a plot was to write a script, run it, and obtain an image file as result. Currently the same image can be easily and directly visible in the notebook, at the cost of the supplementary "rewiring" statement above. </p>

<p>In order to display any plot in the notebook, you can have the <code>plot</code> statement as last line of that block code (i.e. the plot is then the returned value, which get rendered by jupyter automatically), or use plt.show() as described by Abdou in the comment. </p>

<p>Also, watch out that you have 2 plots in your code:</p>

<pre><code># Put these 2 in two separate notebook blocks to get 2 separate plots.
# As-is the first one will never get displayed
plt.plot(x, np.sin(x), label=r'$\sin(x)$') 
plt.plot(x, np.cos(x), 'ro', label=r'$\cos(x)$') 
</code></pre>

<p>If you want to have all plots rendered as one single image (which quickly gets hairy with matplotlib imho), have a look at <a href="http://matplotlib.org/examples/pylab_examples/subplots_demo.html" rel="nofollow">subplot documentation</a></p>

<p>To make the result prettier, include a <code>;</code>at the end of the plot to avoid the ugly 
      <code>&lt;matplotlib.legend.Legend at 0x1124a2fd0&gt;</code></p>

python matplotlib ipython