Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm playing with ipython notebook and I got a problem.

This code %matplotlib inlinehelped me to plot inline with the code below.

%matplotlib inline 
ax1= plt.subplot(2,1,1)
ax1.plot(df.Close,label="sp500")
ax1.plot(ma,label='50MA')
plt.legend()

ax2=plt.subplot(2,1,2, sharex = ax1)
ax2.plot(df['H-L'],label='H-L')
plt.legend()

However, I cannot plot inline with the code below.

%matplotlib inline

def single_stock(stock_name):
    df = pd.read_csv('stocks_date_modified.csv',index_col='time',parse_dates=True)
    df = df[df.type == stock_name.lower()]
    _500MA= pd.rolling_mean(df['value'],500)
    ax1= plt.subplot(2,1,1)
    df['close'].plot(label='Price')
    plt.legend()

    ax2= plt.subplot(2,1,2, sharex = ax1)
    _500MA.plot(label='500MA')
    plt.legend()

    plt.show()

single_stock('bac')

I got an error message saying

UsageError: unrecognized arguments: #this code is to plot inline the notebook

Without the %matplotlib inlineI don't have problem showing the plots but in the popup window.

Could someone help me to solve this?

share|improve this question
    
is there any difference if you run your notebook with ipython notebook --matplotlib=inline? – Anzel Jan 4 '15 at 2:23
    
Thanks a lot! Oh so ipython notebook users type --matplotlib=inline every time when they want the plots to be inline? – user3368526 Jan 4 '15 at 5:38
    
I've posted an answer for your comment :) – Anzel Jan 4 '15 at 5:43
    
Can you post the full traceback? You should only need to run the inline magic once per notebook. – tacaswell Jan 4 '15 at 7:12
    
Thanks everyone :) solved! – user3368526 Jan 4 '15 at 7:35
up vote 1 down vote accepted

You can run your notebook with:

ipython notebook --matplotlib=inline

And to avoid repetitive typing on everytime you use notebook, you can create a notebook profile READ HERE. You can also read this relevant SO Answer with regards to how to write your configuration file.

Or if you're using some sort of shortcut key to invoke your notebook (like in Mac/Linux), you can bind the key combinations to run the command above.

share|improve this answer

If anybody else encounters this error, it seems that you cannot have inline comments on lines that call IPython Magics:

In [9]: %matplotlib inline # allows matplotlib to be inline

UsageError: unrecognized arguments: # allows matplotlib to be inline

Without the comment, it works just fine:

In [9]: %matplotlib inline

But yes, loading matplotlib inline at startup with ipython notebook --matplotlib=inline is probably a wise move anyway.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.