Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MatPlotLib not working with Ubuntu 18.04 and TkAgg (Plot window looks like a black square). #13988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
netzo92 opened this issue Apr 19, 2019 · 35 comments
Labels

Comments

@netzo92
Copy link

netzo92 commented Apr 19, 2019

Bug report

Bug summary

When using MatPlotLib with the TkAgg backend on Ubuntu 18.04, with either Python 2.7 or 3.5, plots are not displayed instead a window that is completely black is displayed.

Code for reproduction

import matplotlib.pyplot as plt
plt.plot((1,4,6))
plt.show()

Actual outcome

The window pops up, except no plot is displayed instead the plot window is just completely back.

Expected outcome

A simple chart of a line-graph going from 1 to 4 to 6. Works on Ubuntu 12, and windows, same versions of MatPlotLib

Matplotlib version

  • Operating system: Ubuntu 18.04 running on an Odroid C1+, the Ubuntu 12 version worked previously on the Odroid C1+
  • Matplotlib version: Tried three versions, the one from apt-get install python3-matplotlib, the one from pip and the latest revision from this git: a3e2897
  • Matplotlib backend (print(matplotlib.get_backend())): 'TkAgg'
  • Python version: 3.5.2 or 2.7.15
  • Jupyter version (if applicable):
  • Other libraries: None
@tacaswell
Copy link
Member

if you do plt.show(block=True) does it work?

My guess is that somehow the tk event loop is not being run.

@netzo92
Copy link
Author

netzo92 commented Apr 19, 2019

if you do plt.show(block=True) does it work?

My guess is that somehow the tk event loop is not being run.

No, same exact output.

@tacaswell
Copy link
Member

do other Tk apps (ex idle) work?

@netzo92
Copy link
Author

netzo92 commented Apr 24, 2019

do other Tk apps (ex idle) work?

Yes, I have an application embedding the some matplotlib plots into a frame, and the rest of the application works fine. Only the plotting doesn't work.

@tacaswell
Copy link
Member

does fig.canvas.draw() work?

That you are embedding this in a bigger Tk application is a rather critical detail. If you are embedding, see https://matplotlib.org/gallery/user_interfaces/embedding_in_tk_sgskip.html .

Does this work as expected from an (i)python prompt without your application?

@netzo92
Copy link
Author

netzo92 commented Apr 25, 2019

No it doesn't work. Fig.canvas.draw() doesn't work. Using pyplot to generate a gui doesn't work.

@tacaswell
Copy link
Member

It is very hard to debug from the amount of information here and without access to the system.

What were the version numbers that you tried? I am not surprised that master branch did not work as it is python 3.6+ . Can you try downgrading Matplotlib versions until it works? We did recently change the way we bind to tk to make it possible for pypy to work.

@netzo92
Copy link
Author

netzo92 commented Apr 26, 2019

I just tried 3.0.3 and 1.5.3. Neither worked, same result with both.

Edit: This was on Ubuntu 18.04 on the odroid c1+

@netzo92
Copy link
Author

netzo92 commented Apr 26, 2019

Doesn't work with Ubuntu 16.04 on either Python 2.7 or Python 3.5

@tacaswell
Copy link
Member

I just tried 3.0.3 and 1.5.3. Neither worked, same result with both.

This suggests that it something in your local configuration. Do any of the other GUI backends work?

@evgenyneu
Copy link

evgenyneu commented Sep 15, 2019

I have similar problem on Ubuntu 18.04.3. I see gray plots. The problem happens frequently, but randomly. Interestingly, an opened gray plot fixes itself if I switch to a different Ubuntu desktop and back, or press "Show Applications" overlay.

I'm using TkAgg backend with Matlotlib v 3.1.1.
Same plotting code works flawlessly on Mac.

matplotlib gray plot on ubuntu

@mauera
Copy link

mauera commented Sep 24, 2019

if you do plt.show(block=True) does it work?

My guess is that somehow the tk event loop is not being run.

You just saved my life! lol thanks

@amimai
Copy link

amimai commented Dec 22, 2019

This is still an issue

pyplot.show() does not work in fresh install of Ubuntu 18.04.3 LTS

results in program hanging and never resolves or faults

tested with several backends to the same results fault looks to be with matplotlib somewhere

@amimai
Copy link

amimai commented Dec 22, 2019

This is still an issue

pyplot.show() does not work in fresh install of Ubuntu 18.04.3 LTS

results in program hanging and never resolves or faults

tested with several backends to the same results fault looks to be with matplotlib somewhere

IDENTIFIED CAUSE
pyplot will not work without a main() function included inside the .py file calling it in ubuntu
this includes imports of modules containing pyplot.show() into the repl

@WeatherGod
Copy link
Member

I seriously doubt that simply adding a def main() had anything to do with matplotlib. Plenty of matplotlib code exists without having such a function in the file. I suspect your original code might have not been executing the way you thought it should.

@Jarvine
Copy link

Jarvine commented Jan 2, 2020

@evgenyneu did you ever resolve the issue? I just upgraded Ubuntu 16.04 to 18.04 and now my plots are temporarily hanging (freezing), showing a grey plot area similar to your image. It happens intermittently and sometimes it hangs for a longer time. Using matplotlib 3.1.2 (problem occurred with 3.1.1, but not prior to the upgrade to 18.04) and Python 3.6.8 in a virtual env. Problem happens in Sublime Text or VSC.

@Jarvine
Copy link

Jarvine commented Jan 2, 2020

To clarify, I'm running Ubuntu 18.04, Python 3.8.6, TkAgg backend (I checked), and Matplotlib 3.1.2 in my virtual environment. As I mentioned, I did not have this problem until I upgraded my system to 18.04 from 16.04. The python scripts run, but the plots, which have a 5 second pause (so that I can examine the output before the next plot is shown) hangsintermittently, showing sometimes a blank black screen or a grey screen before the plot is correctly shown. The duration varies, but if I wait long enough it unhangs and the script continues. Sometimes, just clicking on the second monitor area appears to cause the script to resume:

plt.show(block=False)
plt.pause(5)
plt.close(fig=None)

I also installed tk:

$ sudo apt-get install python3-tk

And have added the extra line below the import line, which I don't think is necessary (and it didn't seem to make a difference):

import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt

@Jarvine
Copy link

Jarvine commented Jan 13, 2020

Does anyone have answer for this? Matplotlib still hangs intermittently for me when I run a script with multiple calls to plot. The program can show 15 or more plots before it hangs (sometimes with a grey or black plot screen). If I do nothing it seems to remain in that condition indefinitely. But if I do other activities on the screen (e.g., type an email, search on the web, etc.), it will resume with the plot and continue with the python script.

I have pip uninstalled and pip re-installed matplotlib and checked the backend as TkAgg. What else?

@Jarvine
Copy link

Jarvine commented Jan 14, 2020

For anyone that may be reading this, I discovered that hitting only the CTRL or shift keys on my keyboard causes matplotlib to resume plotting the figure until it hangs again. Also, I commented out
plt.show(block=False)
plt.pause(5)
and while the plot window no longer opens, the Python script still hangs intermittently until I press either the CTRL or shift keys on the keyboard, then the code resumes.

@catpatris
Copy link

plt.show(block=True) resolved this for me. Thanks.

@SanmithraMudigonda
Copy link

plt.show(block=True) resolved this for me. Thanks.

Hi, Are you now able to see the plots in a window by adding this line in your code? I've been facing the same issue and I'm trying to fix it. TIA.

@Jarvine
Copy link

Jarvine commented Nov 23, 2020

@SanmithraMudigonda I was never able to resolve this issue. Presently, I am using a different computer (different Nvidia graphics card), but still running Ubuntu 18.04. However, I'm running Python 3.6.9 and don't have this problem - even while running the exact codes! I cannot explain why it works on a different machine. My guess it probably has something to do with the driver used for the graphics. I hope you are able to resolve this.

@SanmithraMudigonda
Copy link

@SanmithraMudigonda I was never able to resolve this issue. Presently, I am using a different computer (different Nvidia graphics card), but still running Ubuntu 18.04. However, I'm running Python 3.6.9 and don't have this problem - even while running the exact codes! I cannot explain why it works on a different machine. My guess it probably has something to do with the driver used for the graphics. I hope you are able to resolve this.

@Jarvine Thanks for the response. I am running Python 3.6.9 on Ubuntu 18.04 too, but on the Windows Susbystem for Linux. I am yet to figure out what works for me here.

@Jarvine
Copy link

Jarvine commented Nov 23, 2020

@SanmithraMudigonda Even if I run the same code on my Macbook pro, Python 3.6.8, I don't have this problem. The problem surfaced when I upgraded to 18.04 from 16.04. Initially, I was running tensorflow on my Ubuntu 16.04, which meant that I had installed Cuda and the nvidia driver manually, based on the tensorflow website recommendation. When switching to 18.04, I suspect that Ubuntu updated the driver to the default for my cards. I really believe it is a driver compatibility issue. Have you updated your driver? Are you using an nvidia graphics card?

@tacaswell
Copy link
Member

@SanmithraMudigonda Do you have an xserver installed? What do you get if you do print(plt.get_backend())? Can you get any GUI programs to work from inside of WSL?

@Jarvine I am very skeptical that it could be driver related, there is enough layers of abstraction between the graphics drivers and mpl that something very very strange would have to be happening for there to be a driver issue that only his us. I suspect in is far more likely to be interactions between tk, its event loop, and the desktop manager.

@jklymak
Copy link
Member

jklymak commented Nov 24, 2020

It might be helpful for debugging if we supplied folks with a dummy program for some of the more popular backends so they can make sure their install is OK? I am too ignorant of the various backends to know what such a dummy program might be, but I think it'd help many users who also don't know how to write a little tK or Qt app.

@anntzer
Copy link
Contributor

anntzer commented Nov 24, 2020

python -mtkinter normally pops up a sample tk window, so there's that. For Qt the following should work:

python -c "from PyQt5.QtWidgets import *; app = QApplication([]); win = QMainWindow(); win.show(); app.exec()"

(or replace import with PySide2, if desired).
Likely one-liners likewise exist for gtk and wx.

@QuLogic
Copy link
Member

QuLogic commented Nov 24, 2020

For GTK:

python3 -c 'from gi.repository import Gtk; win = Gtk.Window(); win.connect("destroy", Gtk.main_quit); win.show(); Gtk.main()'

@SanmithraMudigonda
Copy link

@Jarvine I haven't explicitly updated any of my drivers while trying to make this work. My machine has a Intel Graphics card.

@tacaswell I do not have an x server installed. You're right, the print(plt.get_backend()) returns agg instead of TKAgg, although I configured it within my virtual environment's MATPLOTLIBRC file, and I thought that should do. Am I supposed to change this somewhere else too? I haven't tried running any other GUI backends within WSL before this.

@anntzer
Copy link
Contributor

anntzer commented Nov 24, 2020

@QuLogic just as a side point, when giving this kind of command lines, I prefer putting double quotes around the outside string (assuming that there are indeed no problematic shell metacharacters in the string) because that makes them also work on Windows (cmd.exe only accepts double quotes, not single quotes).

@jklymak
Copy link
Member

jklymak commented Nov 24, 2020

@SanmithraMudigonda If tkinter fails to import, then we will fallback (eventually) to agg. There was likely a warning in your console somewhere about this. So you probably need to debug our tk install.

@tacaswell
Copy link
Member

@SanmithraMudigonda See https://matplotlib.org/tutorials/introductory/usage.html?highlight=what%20backend#backends for details about backends. Given that you specified a non-interactive backend, not getting a window is the expected behavior (even if it is not the behavior you wanted!). You are having a different problem that the OP, if the link above does not help you can you please post at https://discourse.matplotlib.org/ ?

@SanmithraMudigonda
Copy link

@jklymak thank you, it does look like an issue with my tkinter installation. I'll check this out.
@tacaswell I understand that now, thank you. I sure will post if I have a problem after checking my tkinter installation as @jklymak suggested.

@github-actions
Copy link

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Jun 14, 2023
@jklymak jklymak removed the status: inactive Marked by the “Stale” Github Action label Jun 14, 2023
@jklymak jklymak closed this as not planned Won't fix, can't repro, duplicate, stale Jun 14, 2023
@TimYi
Copy link

TimYi commented Aug 31, 2023

This issue is still troubling me, and none of the backends are working on my Ubuntu 18.04 system.

@jklymak jklymak mentioned this issue Aug 31, 2023
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests