matplotlib / matplotlib/matplotlib

plt.hist() fails with TensorFlow Numpy emulation

Open
#19,574 28 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

keep third-party integration topic: units and array ducktypes
Dominant language
Python
Stars
23.2k
Forks
8.5k
Avg merge
1d 6h
Merged PRs (30d)
66

Description

### Bug report

**Bug summary**

Generating `np.random.randn(1000)` values, visualizing them with `plt.hist()`. Works fine with Numpy.

When I replace Numpy with tensorflow.experimental.numpy, Matplotlib 3.3.4 fails to display the histogram correctly. Matplotlib 3.2.2 works fine.

**Code for reproduction**

```python
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tensorflow.experimental.numpy as tnp

# bad image
labels1 = 15 + 2 * tnp.random.randn(1000)
_ = plt.hist(labels1)

# good image
labels2 = 15 + 2 * np.random.randn(1000)
_ = plt.hist(labels2)
```

**Actual outcome**

![np-bad](https://user-images.githubusercontent.com/901867/109069736-7b499280-76a6-11eb-87c4-2880aaf759f5.png)

**Expected outcome**

![np-good](https://user-images.githubusercontent.com/901867/109069769-843a6400-76a6-11eb-9d2e-149fc615c583.png)

**Matplotlib version**

* Operating system: Windows 10
* Matplotlib version (`import matplotlib; print(matplotlib.__version__)`): 3.3.4
* Matplotlib backend (`print(matplotlib.get_backend())`): module://ipykernel.pylab.backend_inline
* Python version: 3.8.7
* Jupyter version (if applicable): see below
* Other libraries: see below

TensorFlow 2.4.1

```
jupyter --version
jupyter core : 4.7.0
jupyter-notebook : 6.1.6
qtconsole : 5.0.1
ipython : 7.20.0
ipykernel : 5.4.2
jupyter client : 6.1.7
jupyter lab : not installed
nbconvert : 6.0.7
ipywidgets : 7.6.3
nbformat : 5.0.8
traitlets : 5.0.5
```

Python installed from python.org as an exe installer. Everything else is `pip install --user`

Bug opened with TensorFlow on this same issue:

https://github.com/tensorflow/tensorflow/issues/46274

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names the plt.hist() entry point but no repository files or tests. First run the supplied reproduction with TensorFlow 2.4.1 and Matplotlib 3.3.4, then compare how the histogram input is handled against Matplotlib 3.2.2. Done means TensorFlow NumPy arrays produce the same correctly scaled histogram as regular NumPy arrays, with a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter, python, tensorflow
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.