tensorflow / models

Models and examples built with TensorFlow
Other
76.95k stars 45.79k forks source link

Matplotlib pyplot not working on a python 3.10 conda environment (Fixed, but unsure how fix works) #11087

Open realharryhero opened 1 year ago

realharryhero commented 1 year ago

Prerequisites

Please answer the following questions for yourself before submitting an issue.

0. The fix

(Also, one can read through 1-5 first.)

Quoting some of my text / images in this report's part 5,

" Changing to the backend before importing tensorflow_models, though, does work:

Screen Shot 2023-09-16 at 10 52 43 AM

And after the rest works nicely. "

1. The entire URL of the file you are using

Not a file, but this notebook.

2. Describe the bug

matplotlib's pyplot does not seem to be working after importing tensorflow_models. On the other hand, it seems to be working before importing.

3. Steps to reproduce

  1. Download VSCode.
  2. Create a new conda environment with python 3.10 (3.10.13?); download tensorflow with this link
  3. Install TF Model Garden using pip; also use pip3 install tf-models-nightly.
  4. On this link, click the "Download notebook" icon that appears next to the "Run in Google Colab" and "View on Github" icons.
  5. Add some extra lines of code, each of which have "plt" as the first letters: Screen Shot 2023-09-16 at 10 39 04 AM
  6. Run the two lines in the screenshot.

4. Expected behavior

Both cells should show the plot, but only the first one does:

Screen Shot 2023-09-16 at 10 40 27 AM

5. Additional context

These extra lines of code show below the plot, which doesn't show if the lines of code creating the plot aren't added.

Screen Shot 2023-09-16 at 10 41 11 AM

Checking the matplotlib backend after importing tensorflow_models shown below it seems to be agg, so it does not plot as it is a non-interactive backend.

Screen Shot 2023-09-16 at 10 43 45 AM

But before importing tensorflow_models, the backend is different:

Screen Shot 2023-09-16 at 10 44 19 AM

Changing to TkAgg, an interactive backend, after importing tensorflow_models, doesn't work:

Screen Shot 2023-09-16 at 10 51 04 AM

Changing to the backend before importing tensorflow_models, though, does work:

Screen Shot 2023-09-16 at 10 52 43 AM

And after the rest works nicely.

6. System information

ChandniJha630 commented 11 months ago

Assign me this issue I would love to contribute

3zhang commented 1 month ago

Not a complete fix but a workaround: Run matplotlib inline immediately after tensorflow_models imported:

import tensorflow_models as tfm
%matplotlib inline

Then every time you use plt to plot, make sure to add plt.show() at the end like:

plt.plot([1,2,3])
plt.show()

plt.show() is necessary to let the plot show up.