Forums

ipython and matplotlib does not plot in console

Hi,

i just started my new account here to learn how to make nice data vizualizations.

My first lines in the ipython console were:

from pylab import *
plot(2,2)

I got an error for matplotlib

<matplotlib.lines.2D at 0x072....>

Why I got this error? Should I make a SSH connection and forward the website (free account) to my computer seing the plots?

Thank you in advance, Ela

[edited by admin: formatting]

Hi there,

See if this helps?

I've found it here: https://www.pythonanywhere.com/wiki/MatplotLibGraphs

But I do not like to save the picture and then call the picture. There exist some ipython directive I guess. I think, in the new jupyter version the matplot inline is also possible.

Is there a way in the nearest future to plot it in the console directly?

Hi there,

You won't be able to display graphics in the console, no. However, we are working on supporting ipython notebooks (ie. using jupyter hub). We cannot confirm the date this will be released yet though.

Hi,

sounds promising. Thanks for the information. I'll keep in touch with your site.

Once ipython notebooks are functional, you'll be able to have inline graphics.

Hi, I just wanted to find out if inline graphics are now possible?

They have always been possible in IPython notebooks.

hi Glenn, any news about this topic? I'm still not able to display pictures directly from the iPython reader with "plt.show()" :(

can you send us a screenshot of the error?

there is no error Harry, there is just no picture with the plt.show() function. I can see it with the save function but it would have been even better if I could see it directly in the Jupyter viewer..

OK, can you instead share some example code so I can try and reproduce the error?

Here it is:

plt.clf()
plt.ylim( -3, 3) 
plt.plot(t_instance[1:], time_series(t_instance[1:]), "b-", markersize=10, label="val A")
plt.plot(t_instance[1:], y[0,:,0], "g-", markersize=10, label="val B")
plt.legend(loc="upper left")
plt.xlabel("Time")

save_fig("plot" + project_title + str(iteration))
plt.show()

I'm going to need some actual data to put into those t_instance and time_series variables?

it doesn't work with any plot, for instance:

plt.plot([1,2,3,4], [1,4,9,16], 'ro')

plt.axis([0, 6, 0, 20])

plt.show()

To be clear - are you doing this in a notebook, or a console?

I'm doing it from the Pythonanywhere's Notebook.

I doesn't work also from a iPython console.

same is happening to me. Even if I use the %matplotlib magic in the Ipython Notebook.

what happens if you run

%pylab inline

before you do the plt.show()

it works :) thanks!

:-D

This isn't working for me:

mport matplotlib.pyplot as plt
%pylab inLine
plt.plot([1,2,3,4], [1,4,9,16], 'ro')
plt.axis([0, 6, 0, 20])
plt.show()

Yields:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-28-119c5663f90a> in <module>()
      1 import matplotlib.pyplot as plt
----> 2 get_ipython().magic('pylab inLine')
      3 
      4 plt.plot([1,2,3,4], [1,4,9,16], 'ro')
      5

/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2161         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2162         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2163         return self.run_line_magic(magic_name, magic_arg_s)
   2164 
   2165     #-------------------------------------------------------------------------

/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
   2082                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2083             with self.builtin_trap:
-> 2084                 result = fn(*args,**kwargs)
   2085             return result
   2086

<decorator-gen-107> in pylab(self, line)

/usr/local/lib/python3.5/dist-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/usr/local/lib/python3.5/dist-packages/IPython/core/magics/pylab.py in pylab(self, line)
    154             import_all = not args.no_import_all
    155 
--> 156         gui, backend, clobbered = self.shell.enable_pylab(args.gui, import_all=import_all)
    157         self._show_matplotlib_backend(args.gui, backend)
    158         print ("Populating the interactive namespace from numpy and matplotlib")

/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py in enable_pylab(self, gui, import_all, welcome_message)
   2988         from IPython.core.pylabtools import import_pylab
   2989 
-> 2990         gui, backend = self.enable_matplotlib(gui)
   2991 
   2992         # We want to prevent the loading of pylab to pollute the user's

/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py in enable_matplotlib(self, gui)
   2937         """
   2938         from IPython.core import pylabtools as pt
-> 2939         gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
   2940 
   2941         if gui != 'inline':

/usr/local/lib/python3.5/dist-packages/IPython/core/pylabtools.py in find_gui_and_backend(gui, gui_select)
    260     if gui and gui != 'auto':
    261         # select backend based on requested gui
--> 262         backend = backends[gui]
    263     else:
    264         # We need to read the backend from the original data structure, *not*

KeyError: 'inLine'

Ok, I got the following to work:

%matplotlib inline
import matplotlib.pyplot as plt

plt.plot([1,2,3,4], [1,4,9,16], 'ro')

plt.axis([0, 6, 0, 20])

plt.show()

right- you tweaked the ordering so that the plt import is after the %inline line right?