Datasets: show_examples fails to import matplotlib.pyplot

Created on 23 Mar 2020  路  9Comments  路  Source: tensorflow/datasets

Short description
show_examples leads to an error: "AttributeError: module 'matplotlib' has no attribute 'pyplot'"

Environment information

  • Operating System: Linux
  • Python version: 3.7.4
  • tensorflow-datasets version: 2.0.0

Reproduction instructions

import tensorflow_datasets as tfds
datasets, info = tfds.load(name='stanford_dogs',
                               with_info=True, split='train')
tfds.show_examples(
    info, datasets, rows=3, cols=3, plot_scale=3.0, image_key=None
)

Additional context
https://github.com/tensorflow/datasets/blob/dcaa4c76905bb9eb14b4e0947d1fce15c48aea0f/tensorflow_datasets/core/visualization.py#L66 is not enough, you need to explicitly import matplotlib.pyplot, see https://stackoverflow.com/questions/14812342/matplotlib-has-no-attribute-pyplot

bug

All 9 comments

@Flamefire, I also encountered this error when running locally but its work fine on colab.

There is a workaround: import matplotlib.pyplot anywhere before this. Maybe colab does this by default?

Yes you are right it works, but we use lazy imports for importing modules so that we dont need to import them externally.

Yes it works fine

def matplotlib(cls):
_try_import("matplotlib.pyplot")
return _try_import("matplotlib")

Please check if it works?

It does work :+1:

okay thanks for this bug I will send PR for fixing it

@Flamefire why it is working on colab ?

I can't say as I don't know what colab does. For an idea see https://github.com/tensorflow/datasets/issues/1696#issuecomment-602602989

Was this page helpful?
0 / 5 - 0 ratings