models icon indicating copy to clipboard operation
models copied to clipboard

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

Open realharryhero opened this issue 1 year ago • 2 comments

Prerequisites

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

  • [ X ] I am using the latest TensorFlow Model Garden release and TensorFlow 2.
  • [ X ] I am reporting the issue to the correct repository. (Model Garden official or research directory)
  • [ X ] I checked to make sure that this issue has not been filed already.

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

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): 2.13.0
  • Mobile device name if the issue happens on a mobile device:
  • TensorFlow installed from (source or binary): this link
  • TensorFlow version (use command below): 2.13.10
  • Python version: 3.10.13 (3.10?)
  • Bazel version (if compiling from source): ?
  • GCC/Compiler version (if compiling from source): ?
  • CUDA/cuDNN version: From this link, cuDNN version should be 8.6.0.163 (CUDA 12.0?)
  • GPU model and memory: NVIDIA GeForce RTX 3090, 24576 MiB (?)

realharryhero avatar Sep 16 '23 15:09 realharryhero

Assign me this issue I would love to contribute

ChandniJha630 avatar Oct 01 '23 16:10 ChandniJha630

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.

3zhang avatar Jul 22 '24 03:07 3zhang