Skip to content

"divide by zero" warning doing a proportional colorbar with only 3 bounds #8534

Closed
@rvilla87

Description

@rvilla87

Bug report

Bug summary
When trying to show a proportional colorbar with only 3 bounds I get a "division by zero" warning. I don't get the warning if I put 4 bounds, like that: bounds = [0, 3, 5, 7].

The thing is, I need a bar that shows me, for example, that having 5 animals, 3 of them are dogs. Am I using correctly the mpl.colorbar function? Here's the code:

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib as mpl
fig = plt.figure(figsize=(3, 2))
ax1 = fig.add_axes([0.1, 0.5, 0.8, 0.2])
cmap = mpl.colors.ListedColormap(['green', 'red'])
bounds = [0, 3, 5]
cb2 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap, boundaries=bounds,
                                spacing='proportional', orientation='horizontal')
cb2.set_label('3 of 5')
plt.show()

Actual outcome

C:\ProgramData\Miniconda3\lib\site-packages\matplotlib\colorbar.py:810: RuntimeWarning: divide by zero encountered in double_scalars
  automin = (y[2] - y[1]) / clen
C:\ProgramData\Miniconda3\lib\site-packages\matplotlib\colorbar.py:811: RuntimeWarning: divide by zero encountered in double_scalars
  automax = (y[-2] - y[-3]) / clen

Expected outcome
No warning output, I think the code is correct.

Matplotlib version

  • Operating System: Windows 10
  • Matplotlib Version: 2.0.0 np112py36_0
  • Python Version: 3.6.1
  • Jupyter Version (if applicable): 5.0.0

Python and Matplotlib installed from conda, default channel.

Thanks in advance!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions