Matplotlib Pyplot breaks on Ubuntu 16.04 - python-2.7

I am currently using Python 2.7 on Ubuntu 16.04 and would like the ability to plot figures using Matplotlib. However, calling any sort of plot command causes the entire script to hang at that line.
** Note ** Before marking this as a duplicate question, please consider that this issue may not be related to the backend that I am using as I have tried every iteration of this solution that I have found on the internet.
A simple script which illustrates my problem:
#!/usr/bin/env python
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
plt.interactive('True')
if __name__ == '__main__':
print 'Hello World!'
plt.figure()
print 'Hello Again World!'
The output of the above script is: Hello World!
The script then hangs at the plt.figure() line, causes 100% cpu usage, and cannot be killed using Ctrl-C. I use "kill" to kill that process.

I never figured out what caused my problem, but my (poor) solution was to reinstall Ubuntu 16.04 and then matplotlib. It is working fine now.
Thanks everyone!

Related

How to use the output of a Linux command as a if/else statement in python

Last week I started using python for writing install scripts for my raspberry pi 3.
I installed debian jessie on my pi and within debian I'm using exagear.
When I start the script, I would like to realize that the code could check in which architecture it's running. With the 'arch' command in Debian I can see it's 'armv71' and within exagear it's 'i686'.
I have two different functions in python, one for the 'armv71' architecture and one for the 'i686' architecture. I would activate them by using a if and else statement.
Could anyone help me solve this problem?
I think you can use subprocess module
import subprocess
myoutput = subprocess.check_output(["arch"])
if myoutput=='x86_64\n':
print 'this is x86'
else:
print 'something else...'

matplotlib.pyplot hangs up at any call

pyplot hangs up at any call, like this one:
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
or this one:
plt.ylabel('some numbers')
One core of the CPU keeps running at 100%.
Any other call using pyplot (plt.*) will give the same result.
I am using ubuntu 16.04.LTS, python 2.7.12 and matplotlib 1.5.3
I reinstalled Ubuntu and it works now. Otherwise nothing helped.
try ipython from anaconda.
What is your PC configuration? 12MHz with 8MB Ram?
Plotting data with matplotlib is an absolute basic task

Using pyinstaller on parmap causes a tkinter matplotlib import error. Why?

Update
If I try downgrading from Pyinstaller 3.2 to 3.1 I instead get the following traceback when I try to run the executable.
I tried adding --hidden-import=collect_submodules('pkg_resources._vendor') to pyinstaller as noted here but it had no effect. Same error. This appears to be due to an issue with setuptools. I'm using 26.0.0. Downgrading to 19.3 that many sources say fixes the issue does indeed fix this issue but then I'm back to the issue I have below.
I have a python 2.7 pyqt4 project I'm trying to turn into an .exe using pyinstaller. I use:
pyinstaller --additional-hooks-rir=. --clean --win-private-assemblies pipegui.py
pipegui.py can be found on github here
I get a working executable and the app appears functional. Here is what the terminal spits while pyinstaller is freezing. However the app crashes when I run particular parts of my program. It crashes and the terminal goes in a loop continually outputting the same traceback below with "Poolworker-X" at the very top continually incrementing:
As you can see tkinter is implicated, despite the word "tkinter" appearing nowhere in my project (using pyqt4). I am making use of matplotlib though and from answers discussed here and here I have added the following to the top of pipegui.py my main script:
from Tkinter import *
import Tkinter
import FileDialog
This however seems to be a step in the wrong direction because after freezing with this (and the same flags as before) my executable wont even open and instead I get this:
Here is pyinstaller's pretty-much identical output while freezeing. Remember all I did was add those 3 import statements above. That's it.
I also tried pyinstaller --additional-hooks=. --clean --win-private-assemblies --hidden-import=Tkinter pipegui.py and it made no difference. I'm completely perplexed as to why trying to import tkinter is doing this. Will fixing this traceback lead me closer to solving the other?
I only figured out after trying all this that the only parts of my executable that are crashing are parts that make use of parmap multiprocessing. Classes that make use of matplotlib but not parmap are working fine.
So please note my question is how the first traceback can be fixed and also why are both matplotlib and tkinter popping up in the traceback despite my code where the crash occurs making use of niether?
Extra notes
I use --clean --win-private-assemblies to fix error code 14001 as per here
Repiklis provided the solution in the comments. Further note that as of January 15 2017 Pyinstaller version 3.2.1 was released. I now use this and it solves this issue along with others like this and this that I could previously only solve by using the developer version. So I highly recommend upgrading to the latest version if you haven't already.

Cannot close plot generated with matplotlib in ipython notebook

I am trying to make a plot using matplotlib in ipython notebook.
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()
I get the desired plot, but when I try to close it, it hangs. I then have to force quit it, and restart the kernel.
If I run the same code from terminal, I can safely close the plot. The error is when using ipython notebook.
I am on OS X El Capitan (10.11.2), python 2.7. The issue began after updating to El Capitan.
Thanks in advance.
When your work in a notebook, it is better to display the plots inline. Turn on the inline mode:
In: [1] %matplotlib inline
Now, plot without the plt.show():
In: [2]import matplotlib.pyplot as plt
In: [3]plt.plot(range(10))
The plot should appear right in the notebook.
For interactive plots in the notebook use mpld3. Just add these lines to your notebook:
import mpld3
mpld3.enable_notebook()

Spyder SymPy Wont Print Symbolic Math

I setup Anaconda 2.0.0 (Win 64).
It has SymPy 0.7.5.
I configured Spyder (2.3.0rc that came with Anaconda) to use symbolic math:
Tools > Preferences > iPython console > Advanced Settings > Symbolic Mathematics
I create a new project and a new file:
# -*- coding: utf-8 -*-
from sympy import *
init_printing(use_unicode=False, wrap_line=False, no_global=True)
x = Symbol('x')
integrate(x, x)
print("Completed.")
When I run this (Python or iPython console) it does not print the integral -- it only prints Completed.
But what is weird is that while in the console that just did the run if I then re-type:
integrate(x, x)
It does print the integral.
So running from a file never prints any symbolic math but typing in the console manually does?
Can anyone help with this issue -- maybe it some sort of configuration?
Thank you!
Running a script is not the same as executing code in IPython. When you run the code in a cell or prompt in IPython, it captures the output of the last command and displays it to you. When you run a script, the script is just run, and the only thing that is displayed is what is printed to the screen.
I don't think there is a way to send the IPython display object (which would be needed to get pretty latex output) from a script, but I may be misunderstanding how spyder executes the code in IPython, or missing some hooks that it has. You can try
from IPython.display import display
display(integrate(x, x))
It is because integrate doesn't print automatically, it just returns the output. You will have to pass it to print function to get the output. Try using following code:
# -*- coding: utf-8 -*-
from sympy import *
init_printing(use_unicode=False, wrap_line=False, no_global=True)
x = Symbol('x')
print(integrate(x, x))
print("Completed.")
In Python console(or IPython console) returned statements are automatically printed.
Update: Use pprint for a nice formatted output.