Showing plots in Jupyter notebooks

4,038 views
Skip to first unread message

Paul Smart

unread,
Nov 25, 2015, 11:59:02 AM11/25/15
to sage-cloud
The plt.show() in this simple example works fine:

plt.plot(range(20), range(20))
plt.show()
print "hello world"

However, this example returns the error: AttributeError: 'AxesSubplot' object has no attribute 'show'


    #Plot results
    data=pd.DataFrame.from_records(analysis['analysis']['results'], index='key')
    plt = data.plot(kind = 'line',title=id['label']+" : "+name, xticks = xkeys)
    plt.set_xticklabels(xlabels, rotation='vertical');
    plt.show()

Can anyone help me to ensure the plot is displayed at the correct time in this second example please?
(otherwise it waits until the end of the cell output)

Thanks,
 Paul

William Stein

unread,
Nov 25, 2015, 12:16:10 PM11/25/15
to sage-cloud
Could you provide a working example? Also, this seems like a pure
Jupyter notebook question, so you might get better help on their
mailing list.

William

>
> Thanks,
> Paul
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-cloud" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-cloud+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sage-cloud/76cfa9da-1aaa-49ba-91c8-5cba1aaf85b9%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Best Regards,
William

Found/CEO of SageMath, Inc.

Professor of Mathematics

Harald Schilly

unread,
Nov 25, 2015, 12:46:40 PM11/25/15
to sage-...@googlegroups.com
Hi, this is actually only a matplotlib question.


On Wed, Nov 25, 2015 at 5:59 PM, Paul Smart <paul....@datasift.com> wrote:
> #Plot results
>     data=pd.DataFrame.from_records(analysis['analysis']['results'],
> index='key')
>     plt = data.plot(kind = 'line',title=id['label']+" : "+name, xticks =
> xkeys)
>     plt.set_xticklabels(xlabels, rotation='vertical');
>     plt.show()

You are confused, because you are assigning the axes object to
​ the variable​
plt in line
​2
.

Here is a link to the api of matplotlib's axes:
http://matplotlib.org/1.4.3/api/axes_api.html

Here is a revised example that works out of the box, very similar to yours -- but I'm missing the data to reproduce it.

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import string
data=pd.DataFrame([1,2,3,4,3,4,3,2,1])
ax = data.plot(kind = 'line',title="label")
ax.set_xticklabels(string.uppercase[:len(data)], rotation='vertical')
plt.show()


-- harald


Paul Smart

unread,
Nov 26, 2015, 5:28:15 AM11/26/15
to sage-cloud
Thanks a million Harald - that works great.
This is what happen when non-developers like me copy and paste working code rather than actually understanding it!

Harald Schilly

unread,
Nov 26, 2015, 5:35:34 AM11/26/15
to sage-...@googlegroups.com

On Thu, Nov 26, 2015 at 11:28 AM, Paul Smart <paul....@datasift.com> wrote:
This is what happen when non-developers like me copy and paste working code rather than actually understanding it!

Yes, the key idea is,that the variable name in itself doesn't convey​ any information. So, you have to do things like "type(plt)" to learn which type the object is, where the variable "plt" is pointing to. 

Then, you would have noticed that the return value of the pandas plot is an Axis object.

PS: In case you are lost and need the current figure or axis of matplotlib: it's plt.gcf() and plt.gca() if plt is this pyplot api you are canonically importing in the beginning.

-- harald



Reply all
Reply to author
Forward
0 new messages