Matplotlib drawings not shown as non-root user - python

Knowing that this is a question, 100times asked, but no solution helps me.
Trying to make a simple plot:
import matplotlib.pyplot as p
p.plot(range(20), range(20))
p.show()
I used several backends in ~/.matplotlib/matplotlibrc, such as GTK, GTKAgg, Qt4Agg, gdk, agg, svg, kairo,...), by adding the line
backend: <backend>
But nothing happens when I try to plot. No error message, simply nothing. I used both the standard console python2.7 and the IPython console.
I'm using CentOS with Gnome Desktop. Matplotlib is 1.2.0.
Do you have any ideas?
NOTE: I realized that it is a problem only as non-root user.

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.

plt.show() from matplotlib does not open a window

I know this question or a related one has been asked many times, but I still have not found a solution: I ssh -X into a linux machine, where I run a conda installation of ipython. After importing matplotlib, I generate a plot (e.g., plt.plot(1,1)) and I want to show the plot in a window, which is forwarded to my local machine with plt.show(). This used to work, but suddenly no longer does: plt.show() does nothing. I guess this is related to a conda update, which I did yesterday, since a conda list --revisions does not show any suspicious recent changes in my environment.
From similar posts, I guess this is related to the matplotlib backend. The default backend 'Agg' generates png's via plt.savefig, but does not produce a plot in its own window, in which I can zoom. I have not managed to change the backend, though:
In [7]: import tkinter
In [8]: import matplotlib
In [9]: matplotlib.use('TkAgg')
In [10]: import matplotlib.pyplot as plt
gives
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running
I have tried various similar things but with no success. Any suggestions?
Thank you!

Matplotlib doesn't show plots on Mac; plt.show() hangs on 'MacOSX' backend

As of late, I can't get my Matplotlib plots to show up. I have a very simple script:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
but nothing ever shows up. If I include the line
plt.show()
then my Python process hangs.
In my ~/.config/matplotlib/matplotlibrc file I have
backend : MacOSX
interactive : True
I'm a little embarrassed to ask this question. I've been a Matplotlib user for many years and have never had this problem. I don't know where to begin to fix this problem. Help!
I'm using Matplotlib 2.0.0 with Python 3.5.2 from Anaconda.
The answer, as pointed out by #ImportanceOfBeingErnest is that the backend configuration for me wasn't working. I'm not sure if I need to install some additional libraries or not. I decided not to use the MacOSX backend and used the Qt5Agg backend. This worked just fine and I didn't have to install any new libraries.
I've just remove the line
interactive: True
enter code here
from my ~/matplotlib/matplotlibrc. It works fine with only backend: MacOSX using the plt.show() command.

Have Pyplot windows appear as topmost window in Windows 7

I am using Anaconda with Python 2.7 in the Spyder environment. When plotting a pyplot, the window hides behind all my other open windows instead of appear in front. How would I make the figure appear in front of all other open windows?
The matplotlib backend is: Qt4Agg
(Spyder dev here) The only backend that have this functionality is the TkAgg one. The code posted by #valentin in his/her answer should work when using that backend.
All other backends miss this possibility. This has been a known limitation of Matplotlib for quite some time, as can be seen in this Github issue.
You can try something like:
fig = plt.figure()
plt.show()
fig.canvas.manager.window.activateWindow()
fig.canvas.manager.window.raise_()
But the desired behaviour is backend dependent.
On tkinter:
fm = plt.get_current_fig_manager()
#bring to front
fm.window.attributes('-topmost', 1)
#allow other windows to cover
fm.window.attributes('-topmost', 0)

Python Graphing on linux system without xserver

I am new to python and trying to explore graphs, could you please help me understand if I can plot graphs on the console using matplotlib on a linux system that has no XSERVER running?
Thanks.
You can use matplotlib with no X server by setting the backend to Agg, PS, PDF or SVG, Cairo or GDK (depending on what kind of file you wish to create). You can set the backend in your matplotlibrc file, which, depending on your installation, may be in a directory such as ~/ or ~/.matplotlib or ~/.config/matplotlib/.
Alternatively, you can set the backend in the script itself.
Be sure to set the backend first, before importing other modules such as pyplot:
import matplotlib
matplotlib.use("Agg")
See this SO question for examples.

Categories

Resources