How do I fix matplotlib which is crashing my MacOS - python

I had to remove and reinstall my environment and since then matplotlib giving me major issues
I have had issues with matplotlib previously as the graphs won't show up so had to use 'tkagg' backend which worked absolutely fine. simple example below
import matplotlib
matplotlib.use("tkagg")
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
filename = 'Data.csv'
df = pd.read_csv(filename)
print('done')
df.plot()
plt.show()
After I removed and reinstalled my environment (plus all libraries) a couple of days ago, my Mac OS crashes every time I run the same code and I am logged out of my Mac.
why do I need to explicitly need to use 'tkagg' in the very first place, and 2nd question why is it not working anymore?
its MacOS Mojave 10.14.6

Related

matplotlib pyplot error in jupyter notebook

Within Jupyter notebook I can import matplotlib but not the pyplot module:
import matplotlib % works
import matplotlib.pyplot as plt % The kernel appears to have died. It will restart automatically.
An alternative test also fails:
import matplotlib
matplotlib.pyplot % AttributeError: module 'matplotlib' has no attribute 'pyplot'
However, I can import the pyplot module from the conda command prompt with no errors:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
I got the same results in both the "base" environment and a virtual environment that I created.
Does anyone know what the problem is? I've tried uninstalling and reinstalling the maplotlib package, as well as Jupyter notebook, and conda itself.
This imports matplotlib module/library and then you are trying looks for an attribute or variable defined in matplotlib library named as pyplot; which does not exist. pyplot is just an interface for you to call other relevant interactive state-based functions to plot.
import matplotlib
matplotlib.pyplot
In my opinion you should stick to naming/importing convention defined in documentations of libraries for coherent code.
import matplotlib.pyplot as plt
This is all you need.
import matplotlib.pyplot as plt % The kernel appears to have died. It will restart automatically.
This means you have to restart your juypter there will be a cmd window that you could have closed I recommend you to restart the juypter and then
import matplotlib.pyplot as plt
if it does not work
open juypter cmd from search bar and write pip install matplotlib after the task is done in jupyter cmd reopen juypter and than use
import matplotlib.pyplot as plt
Not that I understand what the problem was, but what worked for me is uninstalling matplotlib with conda, and re-installing it with pip. Oftentimes, the conda versions are not the latest, and there is probably a problem with the specific version. As of matplotlib 3.6.1 pyplot does work in jupyter (I tried with base), though reinstallation of kiwisolver might be needed as well.

ImportError: no module named miniconda

I've installed miniconda on my mac following the instructions from the python website. However, when I write any new script and try and import miniconda, matplotlib or Pandas, I get the error above.
My script so far
from miniconda import *
from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns
CORRECTED
my code is now
from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns
I'm now getting the same import error for pandas and seaborn.
UPDATED:
Turns out the corrected code above works and the issue was with Python runner. The code runs when executed through the terminal. Probably an issue with permissions!
Miniconda is not a module indeed it is a variant of Anaconda python distribution, hence you could not import it. You could get further info about miniconda from the link below.
https://conda.io/miniconda.html

Plot Window Hangs or is Empty in Python

I am having trouble with plots in a Jupyter Notebook in Python 3.5 on Mac OSX. The following code will hang when executed:
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline
myfig = plt.plot(range(5))
plt.show()
If I restart the kernel and un-comment '%matplotlib inline', I do get plots to work inline. However, I'd like to be plotting in a separate window.
If I insert the following code at the beginning:
import matplotlib
matplotlib.use('Agg')
then restart the kernel and run, the code will not hang, but nothing will be plotted, no window opened.
Details:
Mac Book Pro running OSX El Capitan
Anaconda Python 3.5 in a Jupyter Notebook
backend is "MacOSX".
There is a post on GitHub mentioning that using Qt4Agg as backend worked...
If it is not available (and if you can), you might want to try using Hombrew to install Python (instead of Anaconda), Qt and/or Gtk with which you'll be able to use matplotlib without problem.

Trying to get matplotlib/numpy to work. Have tried different solutions and out of ideas

I'm trying to run a basic matplotlib script to ensure that everything is working. The program is as follows:
import numpy as np
import matplotlib.pyplot as plt
x=np.arrange(0,5,0.1);
y=np.sin(x)
plt.plot(x,y)
But when I run "python a.py" in the terminal I am getting the error
that the numpy module has no attribute 'arrange'.
I uninstalled numpy and matplotlib and reinstalled them. First numpy, then matplotlib through the ubuntu repository but I am still getting this error. I can't figure out what is wrong. Am I getting an incomplete installation or something? I am using Ubuntu 14.04.
The method name is arange, not arrange.
Also, after using plt.plot(...), you need to call plt.show() to draw the plot.

matplotlib does not work in Eclipse

I seem to have a problem that is in parts very similar to the one mentioned here:
Python with eclipse import problem
But unfortunatly just in parts otherwise that would have solved mine as well.
I use Eclipse SDK, Version: 3.7.0 with PyDev 101.
Furthermore I have installed
numpy-1.6.1rc1-win32-superpack-python2.6.exe
and
matplotlib-1.0.1.win32-py2.6.exe
as noted here:
http://matplotlib.sourceforge.net/users/installing.html
I have rebuild all the packages and looks the site-packages are listed.
(by the way as you see it is an Python version installed with ArcGIS )
If I test a script for instance a very simple one like:
import numpy
import matplotlib
import pylab as pl
I get the following error in Eclipse:
import matplotlib
import pylab as pl
from matplotlib.pylab import *
ImportError: No module named pylab
Even though the interpreter for Pydev is pointing to the appropriate version of python and matplotlib is installed properly in there (site-packages) it does not work in Eclipse. In iPython it works perfect.
What still needs to be done to get matplotlib work in Eclipse?
Thanks a lot!
Werner
pylab is in matplotlibs namespace, so this should work:
import matplotlib.pylab as pylab
I found that turning off interactive move and then calling show worked.
import matplotlib.pyplot as plt
#...your code...
plt.ioff()
plt.show()

Categories

Resources