python matplotlib backend configuration - python

I just installed Intel Python Distribution with Anaconda. I was trying the plot some data but it plots on Web Browser and gives "Press Ctrl+C to stop WebAgg server" output also. I tried to change the backend using "plt.switch_backend('TkAgg')" and it worked and plotted in standard GUI Tkinter as I wanted. However I want to change the default configs of matplotlib not to put "plt.switch_backend('TkAgg')" everytime I plot something. Do you know how to do it?
Thanks in advance.

Related

How to change the default backend in matplotlib from 'QtAgg' to 'Qt5Agg' in Pycharm?

Qt5Agg is necessary to use the mayavi 3D visualization package. I have installed PyQt5 and mayavi using pip in a separate copied conda environment. The default backend then changes from TkAgg to QtAgg. This is a bit weird because in an earlier installation in a different PC the default changed directly to Qt5Agg. I always check the backend using the following commands from the python console :
import matplotlib
matplotlib.get_backend()
Even with the backend being 'QtAgg', I am able to use mayavi from the terminal without any issue but not when I do so in Pycharm. Here I get a non-responsive empty window (image below) :
Image of the non-responsive window
I have been able to get rid of this issue by explicitly using Qt5Agg instead of QtAgg before the plt call :
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
But I would prefer a better way than using the above in every script that I write. As I had mentioned earlier, I already have mayavi installed and have used it successfully it in Pycharm in a different PC and there the default backend is 'Qt5Agg' and hence there is no need to change the backend explicitly.
Is there anything obvious that I'm overlooking ? Can you please let me know of a way to change the default backend for matplotlib from QtAgg to Qt5Agg after PyQt5 installation using pip ?
Thanks in advance !!
Thanks to #PaulH's comment, I was able to solve the issue. Owing to #mx0's suggestion, I shall now explicitly mention the fix below so that others can also benefit from it.
In a particular conda environment, if matplotlib package is installed, then there will be a 'matplotlibrc' file stored somewhere that defines what the default backend will be whenever matplotlib is imported from that conda environment. The location of this 'matplotlibrc' can be found using the following commands :
import matplotlib
matplotlib.matplotlib_fname()
Please look into the following link if there's any deprecation issue with the above commands :
https://matplotlib.org/stable/tutorials/introductory/customizing.html#customizing-with-matplotlibrc-files
Once the location of the 'matplotlibrc' file is known, open it and simply uncomment one line inside this file. Just change the backend from :
##backend: Agg
to :
backend: Qt5Agg
And that's it. All the plot window troubles in PyCharm will be solved as far as the mayavi 3D visualization package is concerned. For any other use, where a specific backend is necessary, you can also set the default to any other backend of choice.

Basic code from plotly's own website doesn't work. How do I handle a Layout property ceasing to exist?

I'm getting an error trying to set the legend title, which should be very basic. Eventually I tried the code on plotly's website, and even that didn't work.
from plotly.graph_objs import Figure, Scatter
fig = Figure()
fig.add_trace(Scatter(x=[1,2,3,4,5], y=[1,2,3,4,5], name='Increasing'))
fig.add_trace(Scatter(x=[1,2,3,4,5], y=[5,4,3,2,1], name='Decreasing'))
fig.update_layout(legend_title='<b> Trend </b>')
fig.show()
The error I'm getting is:
`Invalid property specified for object of type plotly.graph_objs.Layout: 'legend_title'
The computer I'm trying it on (in Jupyter Notebook) is running Python 3.6.5 and plotly 4.4.1. The code does work on a different computer running Python 3.7.1 and plotly 4.6.0. However, all of those versions seem relatively new. For reasons, I can't upgrade the Python or plotly versions of the computer that I can't get it to work on (which is the computer that I need to get it to work on). Is my plotly corrupted? I tried '!pip install plotly' but that didn't do anything. I then tried "!pip uninstall plotly' but that just locked up the cell at [*]. Is there another way to reinstall plotly? Or is there something else that I'm missing?
legend_title was introduced in Plotly v4.5 so yes, you will need to upgrade: https://community.plotly.com/t/announcing-plotly-py-4-5/34045

Bokeh + interactive widgets + PythonAnywhere

I was unable to find a minimal working example for an interactive web app using bokeh and bokeh widgets that runs on PythonAnywhere.
Ideally, I would like to have a simple plot of a relatively complicated function (which I do not know analytically, but I have SymPy compute that for me) which should be replotted when a parameter changes.
All code that I have found so far does not do that, e.g. https://github.com/bokeh/bokeh/tree/master/examples, or refers to obsolete versions of bokeh.
Most of the documentation deals with running a bokeh-server, but there is no indication on how to have this running with WSGI (which is how PythonAnywhere handles the requests). For this reasone I have tried embedding a Bokeh plot within a Flask app. However, as far as I understand, in order to have interactive Bokeh widgets (which should trigger some computation in Python) do require a bokeh-server. I am not particularly attached to using either Flask or Bokeh, if I can achive a similar result with some other simpler tools. Unfortunately, a Jupyter notebook with interactive widgets does not seems to be an option in PythonAnywhere.
I have installed bokeh 0.12 on Python 3.5.
I have managed to run a simple bokeh plot within a flask app, but I am unable to use the Bokeh widgets.
Here is a working example of Jupyter notebook with interactive widgets on pythonanywhere:
%pylab inline
import matplotlib.pyplot as plt
from ipywidgets import interact
def plot_power_function(k):
xs = range(50)
dynamic_ys = [x ** k for x in xs]
plt.plot(xs, dynamic_ys)
interact(plot_power_function, k=[1, 5, 0.5])
PythonAnywhere does have the ipywidgets module pre-installed. But if you are not seeing the interactive widgets, make sure that you have run jupyter nbextension enable --py widgetsnbextension from a bash console to get it enabled for your notebooks. You will have to restart the jupyter server after enabling this extension (by killing the relevant jupyter processes from the consoles running processes list on the pythonanywhere dashboard).
As of Bokeh 0.12.5 you can embed Bokeh server applications directly in Jupyter notebooks. This is the best and most robust way to have interactive Bokeh plots and widgets (backed by real python code) in a notebook.
You can study an example of this in this demo notebook:
https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb
A screencast of that notebook in action is below:

Python Tkinter issue using with ubuntu server

I have problem in using tkinter plugin.
I am using ubuntu 12.10 desktop in my local system and i installed python 2.7 and 3.2
I used tkinter plugin in OpenERP it works fine in my local pc.
But the problem is when use same code in server (Ubuntu 12.04 Server) it doesn't works
It shows
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk,sync, use)
TclError: no display name and no $DISPLAY environment variable
I check with python in terminal also it says error it means there is problem in tkinter
somewhere i dont know.
I also checked for tkinter and python versions both are good.
But i find some thing unusual while locating tkinter in terminal.
In Local it show tkinter plugin located in python3.2,
But in the server it doesn't show the tkinter
I checked the permission also it's fine.
How to make tkinter working in Ubuntu server 12.04
It would have been nice to include the full traceback and a larger code extract. The error message means that Tkinter is unable to find your X graphical environment, which likely means you're running the code on a headless server, as Dominic pointed out.
You could try to install X on the server, but there's no need for a GUI if the machine is really meant to be a server with no user interface.
If you do need to display a GUI when that code is executed, then it should either not run on the server-side or you need to setup X11-forwarding to the client machine. This does not seem to make a lot of sense in an OpenERP context though - as it would hardly work in a generic manner for all users.
Now are you perhaps using matplotlib to produce graphics (your code extract does not show that)? If yes, you can simply force it to use a headless backend for producing the images, as explained in this question or this other question.
Try to have the following code executed before the rendering code is called:
import matplotlib
import matplotlib.pyplot
# force headless backend, or set 'backend' to 'Agg'
# in your ~/.matplotlib/matplotlibrc
matplotlib.use('Agg')
# force non-interactive mode, or set 'interactive' to False
# in your ~/.matplotlib/matplotlibrc
matplotlib.pyplot.ioff()
See also the following references:
What is a matplotlib backend
Customizing matplotlib with a matplotlibrc file
I just had the same problem, for those still interested: the existing answer is almost right, but the order is important: you need to call matplotlib.use before pyplot.
This worked for me:
import matplotlib
# force headless backend, or set 'backend' to 'Agg'
# in your ~/.matplotlib/matplotlibrc
matplotlib.use('Agg')
import matplotlib.pyplot
# force non-interactive mode, or set 'interactive' to False
# in your ~/.matplotlib/matplotlibrc
matplotlib.pyplot.ioff()

python's save figure button does not work in mac, trying to solve it unsuccessfully, how to do this?

I updated my python distribution yesterday to EPD 7.3-2 (64-bit). I am working on a mac with snow leopard.
Now the plot device of matplotlib is broken in at least two ways:
the "save" button doesn't work and makes the terminal or ipython crash and
the only way to see the figure is to have it in front of you, there is no python figure icon in the dock.
I did my homework and these same problems were reported here and here.
I tried to follow the instructions to fix this given in here, but this is the error that I get:
$python install_pythonw.py `which python`/../..
/Library/Frameworks/EPD64.framework/Versions/Current/.Python does not exist; exiting.
Indeed, I looked at the given folder and I could not find a .Python file. I added a comment at the answer to this problem but so far no one has replied to it :( :(
Any idea of how to fix this?
thanks!
I have seen this problem a few times, and it seems to be a problem in some backends. Also, it doesn't seem normal that your session crashes after 4 or 5 plots. In particular, the MacOSX backend seems buggy.
As you installed the EPD, I think it's less likely that your installation is broken.
The solution seems to be using a different backend. You can try with ipython --pylab a few backends, try their features and see if the save button works. You can try the following:
ipython --pylab=wx
ipython --pylab=tk
ipython --pylab=osx
The last one is the option that you're probably using right now, so perhaps not the best. If you just call ipython --pylab, it will use the default backend from your ~/.matplotlib/matplotlibrc file. Once you find a working backend you can change the default by editing that file. Look for a line like this:
backend : MacOSX
(your version may have a different backend.) Just change that setting to WXAgg, TkAgg, or Qt4Agg. With the --pylab option the names are slightly different, they don't have the Agg part. My favourite backend for OSX is the Qt4Agg backend, but I don't think it ships with EPD and the save button also doesn't work! But either WXAgg or TkAgg should work fine.
Other ways of changing the backend in a script are:
import matplotlib
matplotlib.use('WXAgg')
or
matplotlib.rcParams['backend'] = 'WXAgg'

Categories