Update matplotlib to matplotlib 1.3 on windows - python

I want to try out the new .xkcd() function for matplotlib as shown here
http://matplotlib.org/xkcd/examples/showcase/xkcd.html
but it is not working because I think it only works for matplotlib 1.3. So I'm wondering how I would update to matplotlib 1.3. I tried
easy_install matplotlib
but that did not seem to work. Alternatively, if there is a way to use .xkcd() without updating, that would answer my question as well. Also, I am using windows.

On this page pick the version of python that you are using, download the exe and run it.

Related

Matplotlib does not show plot in PyCharm on Mac

I am using PyCharm on my MacBook to code and now I wanted to make a simple plot. When I run the file via the usual 'Run' command (do not know what it is called), it nicely shows my plots, but when I run the file in the Python console (which I find more convenient because you can access your variables) it does not show anything. On the other hand, when I just type it in the Python console afterwards, it does work.
I have read some things about backends and other 'solutions' as I am apparently not the only one with this issue. Mine says macosx and gives the command: "Backend MacOSX is interactive backend. Turning interactive mode on." after running the file in the Python console. I tried changing the backend:
import matplotlib
# matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
but that does not work (no plot pops up). And yes, I use plt.show() after my plotting section :)
I also tried with 'QtAgg' but then I get: "ImportError: Failed to import any qt binding"
Now I am completely new to this backends stuff (I think it has to do with this), so I could really use some clear directions on how I can solve this issue.
Thanks in advance!
I am not sure we can solve this bug with some adjustment. I think you need to fresh start. I suggest you to start a new clean venv and install a new matplotlib there.

Which backend for matplotlib using MacOS?

The question of using matplotlib with MacOS is a tricky one which has already been thoroughly reviewed by a number of discussions (see below). The problem is the following:
using MacOS Mojave 10.14.3
using python 3.7.2 in a conda environment
using matplotlib 3.0.3
Here is the simplest code snippet I came up with which allows reproducing the issue:
from matplotlib import pyplot as plt
x = [1, 2, 3]
y = [1, 2, 3]
plt.plot(x, y)
plt.show()
This throws the following error:
2019-03-22 12:25:43.429 python3.7[22209:554135] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85866b9de0
2019-03-22 12:25:43.431 python3.7[22209:554135] \*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85866b9de0'
*** First throw call stack:([...])
libc++abi.dylib: terminating with uncaught exception of type NSException
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
The issue is documented here. One solution is to install the PyQt5 package to your Python installation and to add the following lines at the beginning of your script:
import matplotlib
matplotlib.use("Qt5Agg")
While this works perfectly well, I am wondering why other backends fail to provide similar behavior.
Indeed I tried using MacOSX backend :
import matplotlib
matplotlib.use('MACOSX')
Which yields to the error:
from matplotlib.backends import _macosx
ImportError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
The issue is documented here, there and in plenty of other threads.
Two solutions came out :
install python.app(conda install python.app) and launch your script with pythonw instead of python
use TKAggbackend
Using the first one works well but I wonder:
why do we need to call pythonw instead of python ?
what exactly is the python.app package ?
how can we make this solution work using an IDE (let say PyCharm for instance) ?
As for the second one, it does "work" up to a certain point: when running matplotlib using TkAgg, the plot window is really buggy. Indeed, it often needs several clicks on the "zoom", "pan" or "home" buttons to get them to actually work. It really is a great pain to use it. I asked several colleagues or friends using matplotlib with TkAgg and they all have the same issue.
Does anyone know the reason for this state of fact? Or if there is any workaround to avoid this issue (apart from installing pyqt5)?
Using the first option is your best bet since you are already working with a virtual environment.
According to matplotlib, there are two variants of python:
A Framework build - Quite important for GUI stuffs in MacOXs
A regular build.
Matplotlib in this case would want to interact natively with OSX and for this, it needs the Framework build this is the reason why installing the python.app type of python is important.
More information can be gotten from Matplotlib FAQ.
Check this link for more about the need for a framework build python.
I'm going to make some assumptions. If they're wrong I apologize.
You installed Python with Anaconda.
Personally, I've never had any problems on mac with matplotlib. My setup is: Mojave, Python3.7.3 in a venv using the python built in module (python3 -m venv), and matplotlib 3.0.3.
I can't answer your question on how to fix your problem, but I'm kind of trying to answer your "is there any workaround" question. Personally, I've always had issues with using Anaconda/Spyder/Conda for Python. I've always felt installing it as its own binary/app on the system leads to the fewest errors.
Now, I'm not saying you have to download and install by hand though. I use homebrew and it saves me headaches everyday I assume (such as upgrading applications and packages). That's the "work around" I'd suggest. Because isn't installing via Anaconda/Spyder already a workaround to installing Python properly? I've always felt performing one work around requires more workarounds for full functionality. Such as having to specify the matplotlib backend when it should be detected by default.
Obviously, I'm a little biased against that tool and that may be reflected in this answer, so take it with a grain of salt. Even though Conda is a legitimate tool that I think is useful, I find it annoying having to use both pip and conda when conda doesn't contain the packages I want.
There are two things you can try.
You can read Matplotlib info page on that, https://matplotlib.org/3.1.0/faq/osx_framework.html,
The default python provided in (Ana)conda is not a framework build. However, a framework build can easily be installed, both in the main environment and in conda envs: install python.app (conda install python.app) and use pythonw rather than python.
And follow the instructions.
Or simply follow the error message you get when you try %matplotlib inline,
(...)
UnknownBackend: No event loop integration for 'inline'. Supported event loops are: qt, qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, osx
I did %matplotlib osx and have plt.imshow(myimg) working just fine afterwards.

What GUI library is matplotlib using?

By the appearance of matplotlib, I guess it is using PyQt; but when I looked into the code of it, I didn't find it importing any GUI library at all. So, what exactly does matplotlib use to show the graph (for example, the window when calling plt.show())?
You can tell which backend matplotlib is using with the get_backend() method:
matplotlib.get_backend()
You can see a list of all available backends with rcsetup.all_backends:
matplotlib.rcsetup.all_backends

Pyplot LaTeX going wild in Python 3

I have a very strange problem that appeared recently, more or less simultaneously to my Ubuntu upgrade to 16.10.
Using Pyplot, when I use LaTeX in labels, Python3 gives back something very strange. For example, I tried to compile the example from
http://matplotlib.org/examples/text_labels_and_annotations/text_demo_fontdict.html
and I got this result:
I try to upgrade my matplotlib with pip, but nothing changed. Right now:
numpy=1.11.1, matplotlib=1.5.3
Interestingly, this problem disappears when I go back to python2, and the conversion is good.
Edit: on another machine, with Ubuntu 16.10 up-to-date, the same mpl and np versions, this bug appears in python3 AND python2.
EEdit : I did not find the problem, but a guess is a problem in the cm font of matplotlib. I tried to change the font (see e.g. How do I change the axis tick font in a matplotlib plot when rendering using Latex?), and the problem disappeared...
Thanks in advance for your help!

How to work with matplotlib?

One of the graph generation files of Python is Matplotlib("http://matplotlib.org/").
I am using Python3.2.1 on Windows7(64bit) O/S.
I am trying to download the same to run matplotlib.animation.
If any one of the esteemed members of the group may kindly suggest me how to download
the same.
Thanks in Advance,
Regards,
Subhabrata Banerjee.
I can not import matplotlib.pyplot/matplotlib.pylab and if I have to download where from I may download? I am using Python3.2.1 on Windows7(64 bit).
But without a separate download how come matplotlib.animation is fine?
Regards,
Subhabrata.
I dont believe full support of python 3 has been added to matplotlib. Here is the status page.
It will support it on the next release.
If you want to use matplotlib with python. I believe you should use python 2.7 because Python 3 has massive changes that broke backward compatibility.
You can use pip to install the matplotlib by following command:
pip install matplotlib
It will automatically handle download and dependency task for you.
For how to install pip under Windows, refer here: How do I install pip on Windows?

Categories

Resources