Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Inconsistent behavior between Normalizers when input is Dataframe #16400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alicanb opened this issue Feb 3, 2020 · 4 comments
Closed

Inconsistent behavior between Normalizers when input is Dataframe #16400

alicanb opened this issue Feb 3, 2020 · 4 comments

Comments

@alicanb
Copy link

alicanb commented Feb 3, 2020

Bug report

Bug summary
It seems matplotlib.colors.Normalize can work with pandas.Series inputs, whereas LogNorm can't.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm, Normalize, LogNorm
import pandas as pd

x = np.linspace(0, 3 * np.pi, 500)
y = np.sin(x)
dydx = np.linspace(0, 3 * np.pi, 500) +1  # first derivative
df= pd.DataFrame({'x':x,'y':y,'dydx':dydx})
points = np.array([df['x'], df['y']]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
fig, axs = plt.subplots()

# Create a continuous norm to map from data points to colors
norm = LogNorm(df['dydx'].min(), df['dydx'].max())
lc = LineCollection(segments, cmap='viridis', norm=norm)
# Set the values used for colormapping
lc.set_array(df['dydx'])
lc.set_linewidth(2)
line = axs.add_collection(lc)
fig.colorbar(line, ax=axs)
axs.set_xlim(x.min(), x.max())
axs.set_ylim(-1.1, 1.1)
plt.show()

Actual outcome

TypeError: '<=' not supported between instances of 'SingleBlockManager' and 'int'

Expected outcome

Not sure about what's expected, but if I do Normalize instead of LogNorm it works, if I do lc.set_array(df['dydx'].to_numpy()) instead of lc.set_array(df['dydx']) it also works. It's the combination of LogNorm and pd.Series is the problem.

Matplotlib version

version 3.1.1
module://ipykernel.pylab.backend_inline

@chowington
Copy link

@alicanb Thanks for reporting this; I experienced exactly the same thing on legacy code. Hope it gets fixed.

@jklymak
Copy link
Member

jklymak commented Mar 31, 2021

Edit: sorry, something was messed up in my install...

@jklymak
Copy link
Member

jklymak commented Mar 31, 2021

The full traceback is

Traceback (most recent call last):
  File "/Users/jklymak/matplotlib/testPandas.py", line 22, in <module>
    fig.colorbar(line, ax=axs)
  File "/Users/jklymak/matplotlib/lib/matplotlib/figure.py", line 1171, in colorbar
    cb = cbar.Colorbar(cax, mappable, **cb_kw)
  File "/Users/jklymak/matplotlib/lib/matplotlib/colorbar.py", line 1170, in __init__
    mappable.autoscale_None()
  File "/Users/jklymak/matplotlib/lib/matplotlib/cm.py", line 474, in autoscale_None
    self.norm.autoscale_None(self._A)
  File "/Users/jklymak/matplotlib/lib/matplotlib/colors.py", line 1558, in autoscale_None
    super().autoscale_None(np.ma.masked_less_equal(A, 0, copy=False))
  File "/Users/jklymak/anaconda3/envs/matplotlibdev/lib/python3.9/site-packages/numpy/ma/core.py", line 2043, in masked_less_equal
    return masked_where(less_equal(x, value), x, copy=copy)
  File "/Users/jklymak/anaconda3/envs/matplotlibdev/lib/python3.9/site-packages/numpy/ma/core.py", line 1015, in __call__
    result = self.f(da, db, *args, **kwargs)
TypeError: '<=' not supported between instances of 'SingleBlockManager' and 'int'

So clearly, our use of np.ma.masked_less_equal(A, 0, copy=False)) is not liked by the data frame. We could likely put some logic in ScalarMappable to normalize to a numpy array, but that ends up being a large game of whack-a-mole. We also end up with issues of converting the array too early and losing the original information etc. But in general, life is simpler if you pass us a numpy array.

@QuLogic
Copy link
Member

QuLogic commented Jul 8, 2021

This has been fixed by #20511.

@QuLogic QuLogic closed this as completed Jul 8, 2021
@QuLogic QuLogic added this to the v3.4.3 milestone Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants