Closed
Description
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