uwhpsc-2016 / lectures

Notes, slides, and code from the in-class lectures.
7 stars 21 forks source link

Lecture 3: matplotlib on Sagemathcloud #1

Open alyfarahat opened 8 years ago

alyfarahat commented 8 years ago

I tried to run the following code on jupyterNotebook and it properly plots the output.

import numpy as np

In [2]: import matplotlib.pyplot as plt

In [3]: x = np.arange(-np.pi, np.pi, 0.01)

In [4]: y = np.sin(x)

In [5]: plt.plot(x, y)

However, when I try to run it from inside ipython in a terminal window or inside a python module, I get the following TclTk error. I do not understand why this is happening.

`TclError                                  Traceback (most recent call last)
<ipython-input-5-0ed05b94d9ab> in <module>()
----> 1 plt.plot(x, y)

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in plot(*args, **kwargs)
   3090 @_autogen_docstring(Axes.plot)
   3091 def plot(*args, **kwargs):
-> 3092     ax = gca()
   3093     # allow callers to override the hold state by passing hold=True|False
   3094     washold = ax.ishold()

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in gca(**kwargs)
    826     matplotlib.figure.Figure.gca : The figure's gca method.
    827     """
--> 828     ax =  gcf().gca(**kwargs)
    829     return ax
    830

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in gcf()
    460         return figManager.canvas.figure
    461     else:
--> 462         return figure()
    463
    464 fignum_exists = _pylab_helpers.Gcf.has_fignum

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)
    433                                         frameon=frameon,
    434                                         FigureClass=FigureClass,
--> 435                                         **kwargs)
    436
    437         if figLabel:

/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager(num, *args, **kwargs)
     79     FigureClass = kwargs.pop('FigureClass', Figure)
     80     figure = FigureClass(*args, **kwargs)
---> 81     return new_figure_manager_given_figure(num, figure)
     82
     83

/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager_given_figure(num, figure)
     87     """
     88     _focus = windowing.FocusManager()
---> 89     window = Tk.Tk()
     90     window.withdraw()
     91

/usr/lib/python2.7/lib-tk/Tkinter.pyc in __init__(self, screenName, baseName, className, useTk, sync, use)
   1815                 baseName = baseName + ext
   1816         interactive = 0`
quantheory commented 8 years ago

I strongly encourage you to use np.linspace, not np.arange. That said, I don't see a similar error. Can you post the specific .py or .ipynb file that is giving you this error?

alyfarahat commented 8 years ago

Thanks! As mentioned above, using this code as is on jupyterNotebook works fine without problems.

The problem happens only when used inside ipython, run from a terminal window. Or if the code is inside a .py script, run from a terminal window too. I followed your recommendation and used np.linspace() instead of np.arange with no difference.

I believe it has to do with the way my account is configured. It would be nice if that's not the case.

The python script is attached as a .txt. practiceMatplotlib.txt

alyfarahat commented 8 years ago

I verified that the previously attached python script runs locally on my computer without errors, but complains on sagemathcloud about TclTk.

quantheory commented 8 years ago

I can't reproduce the TclTk error; if you are curious about that, please send the exact series of commands you used on SMC.

However, if you do not have a "display" for the plot, I don't think that you can use the plt.plot command anyway. I'm not sure why you are trying to avoid Jupyter when you run a plotting script on SageMathCloud, but this may help you:

https://stackoverflow.com/questions/15713279/calling-pylab-savefig-without-display-in-ipython

alyfarahat commented 8 years ago

The exact sequence matches exactly what I sent in the python script attached. Did you manage to run the script from the terminal and get the display to work?

Yes my issue is not being able to get matplotlib.pytplot.plot to run from inside a python script on sagemathcloud. I am not trying to avoid jupyterNotebook, I am just assuming that a syntactically correct python script should run.

Thanks for the stackoverflow link, I'll take a look.

quantheory commented 8 years ago

Did you manage to run the script from the terminal and get the display to work?

I did try from the terminal, in 4 different ways, but I did not get a TclTk error, which is why I asked for the commands you ran from the terminal. The Python script that you gave is not giving me that problem, so I need to know how you ran it.

However, I did get a different error due to the fact that SageMathCloud has no display for a bare Python command. If you are not using a Jupyter notebook, the process does not have any display to plot on, so you get an error. Technically speaking, the Python script is working perfectly, but the environment does not provide it with any way to display the output, so it has to produce an error. matplotlib.pyplot.plot should not work without an X display (or some equivalent). The link I provided should explain a way around it, namely two methods of creating a plot that can be saved without trying to display the output to a screen.

alyfarahat commented 8 years ago

Okay, now I understand. The TclTk error showed up when I ran the commands in the script inside ipython, but ran this instance of ipython from the terminal, and not in JupyterNotebook.

Should I conclude that, in general, to be safe regarding running our python scripts, we should run the tests from jupyterNotebook but not the terminal?

quantheory commented 8 years ago

I would recommend that you keep any plotting commands specifically in a Jupyter notebook, or that you turn off interactive plotting in your scripts and simply save a file instead. However, it's fine to run the tests from the terminal as long as they don't produce plots.

Personally, I would separate things out like so:

  1. Make homework1.exercise1, homework1.exercise2, and homework1.exercise3 modules, which contain only the numerical code, which can be tested with the test suite.
  2. Make a separate Jupyter notebook or Python script that imports the contents of these modules and uses them to make the plots for your report.