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

Skip to content

matplotlib.colors.LogNorm fails when using pd.Sequence #20141

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
lwelzel opened this issue May 3, 2021 · 3 comments
Closed

matplotlib.colors.LogNorm fails when using pd.Sequence #20141

lwelzel opened this issue May 3, 2021 · 3 comments

Comments

@lwelzel
Copy link

lwelzel commented May 3, 2021

Bug report

Bug summary

When trying to pass a pandas.Sequence to matplotlib.colors.LogNorm to set the norm of a LineCollection I expect it to handle it similar to matplotlib.colors.Normalize. However the LineCollection is not properly set.

This might also be an issue with using LineCollection.set_array() but in any case I would expect both Normalize and LogNorm to handle this similarly.

Code for reproduction

import numpy as np
import pandas as pd
from matplotlib.colors import LogNorm, Normalize
from matplotlib.collections import LineCollection
import matplotlib.pyplot as plt
x=np.arange(1, 11)
y=np.arange(1, 11)
z=pd.Series(np.logspace(-5.0, 9, num=10))

points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

norms = [plt.Normalize(vmin=z.min(), vmax=z.max()),
         LogNorm(vmin=z.min(), vmax=z.max()),
         Normalize(vmin=z.min(), vmax=z.max())]

# the different cases I tried: 
for norm in norms:
    print(f"Trying: {type(norm)}")
    fig, ax = plt.subplots(constrained_layout=True)
    try:
        lc = LineCollection(segments, cmap='viridis', norm=norm)
        
        lc.set_array(z)
        lc.set_linewidth(10)
        line = ax.add_collection(lc)
        fig.colorbar(line, ax=ax)
        
        ax.plot(x, y)
        fig.show()
    except TypeError as e:   
        print(e)
    plt.clf()

# and finally the part that causes the issue: 
norm = LogNorm(vmin=z.min(), vmax=z.max())
fig, ax = plt.subplots(constrained_layout=True)
lc = LineCollection(segments, cmap='viridis', norm=norm)

lc.set_array(z)
lc.set_linewidth(10)
line = ax.add_collection(lc)
fig.colorbar(line, ax=ax)
        
ax.plot(x, y)
fig.show()

Actual outcome

see the error being raised above

Expected outcome
Using a np.array instead of a pd.Sequence

import numpy as np
import pandas as pd
from matplotlib.colors import LogNorm, Normalize
from matplotlib.collections import LineCollection
import matplotlib.pyplot as plt
x=np.arange(1, 11)
y=np.arange(1, 11)
### now z is set as an np.array
z=np.logspace(-5.0, 9, num=10)

points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

norm = LogNorm(vmin=z.min(), vmax=z.max())

fig, ax = plt.subplots(constrained_layout=True)
lc = LineCollection(segments, cmap='viridis', norm=norm)

lc.set_array(z)
lc.set_linewidth(10)
line = ax.add_collection(lc)
fig.colorbar(line, ax=ax)
        
ax.plot(x, y)
fig.show()

Matplotlib version

  • Operating system: Win 10
  • Matplotlib version: 3.4.1
  • Matplotlib backend: module://backend_interagg
  • Python version: 3.8
  • Other libraries: pandas
@jklymak
Copy link
Member

jklymak commented May 3, 2021

Can you clarify what the error is?

@jklymak
Copy link
Member

jklymak commented May 3, 2021

Actually I think this is a duplicate of #16400, I'll close, but feel free to request a re-open if I'm mistaken. And do feel free to comment there.

@lwelzel
Copy link
Author

lwelzel commented May 3, 2021

That is indeed the same issue. I didnt find the other report before.

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

2 participants