Skip to content
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

AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap' with Matplotlib 3.9 #1121

Closed
QuLogic opened this issue Apr 29, 2024 · 2 comments · Fixed by #1122
Closed

Comments

@QuLogic
Copy link

QuLogic commented Apr 29, 2024

The matplotlib.cm.get_cmap function was deprecated in Matplotlib 3.7: https://matplotlib.org/stable/api/prev_api_changes/api_changes_3.7.0.html#deprecation-of-top-level-cmap-registration-and-access-functions-in-mpl-cm

It is being removed in 3.9, which causes a test failure:

___________________________ test_example[filepath1] ____________________________

filepath = PosixPath('/builddir/build/BUILD/NeuroM-3.2.10/examples/density_plot.py')

    @pytest.mark.parametrize("filepath", EXAMPLES_DIR.glob("*.py"))
    def test_example(filepath):
    
        spec = importlib.util.spec_from_file_location(filepath.stem, filepath)
        module = spec.loader.load_module()
    
        with tempfile.TemporaryDirectory() as tempdir:
    
            # change directory to avoid creating files in the root folder
            try:
                cwd = os.getcwd()
                os.chdir(tempdir)
>               module.main()

tests/test_examples.py:25: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
examples/density_plot.py:117: in main
    plot_density(neurons)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

population = <neurom.core.population.Population object at 0x7fdf028645f0>
bins = 100, new_fig = True, subplot = 111, levels = None, plane = 'xy'
colorlabel = 'Nodes per unit area', labelfontsize = 16, color_map = 'Reds'
no_colorbar = False, threshold = 0.01
neurite_type = <NeuriteType.basal_dendrite: 3>, kwargs = {}
fig = <Figure size 640x480 with 1 Axes>, ax = <Axes: >
H1 = array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]])
xedges1 = array([-150.66921768, -147.69081765, -144.71241761, -141.73401757,
       -138.75561753, -135.77721749, -132.79881745,... 129.30038604,  132.27878608,
        135.25718612,  138.23558616,  141.2139862 ,  144.19238624,
        147.17078628])
yedges1 = array([-150.48435587, -147.43490588, -144.38545588, -141.33600588,
       -138.28655589, -135.23710589, -132.18765589,... 136.16394384,  139.21339384,
        142.26284383,  145.31229383,  148.36174383,  151.41119382,
        154.46064382])
mask = array([[ True,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ...,  True,  True,  True],
      ...True],
       [ True,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ...,  True,  True,  True]])
H2 = masked_array(
  data=[[--, --, --, ..., --, --, --],
        [--, --, --, ..., --, --, --],
        [--, --, --, ..., ...  True,  True, ...,  True,  True,  True],
        [ True,  True,  True, ...,  True,  True,  True]],
  fill_value=1e+20)

    def plot_density(population,  # pylint: disable=too-many-arguments, too-many-locals
                     bins=100, new_fig=True, subplot=111, levels=None, plane='xy',
                     colorlabel='Nodes per unit area', labelfontsize=16,
                     color_map='Reds', no_colorbar=False, threshold=0.01,
                     neurite_type=NeuriteType.basal_dendrite, **kwargs):
        """Plots the 2d histogram of the center
           coordinates of segments in the selected plane.
        """
        fig, ax = matplotlib_utils.get_figure(new_fig=new_fig, subplot=subplot)
    
        H1, xedges1, yedges1 = extract_density(population, plane=plane, bins=bins,
                                               neurite_type=neurite_type)
    
        mask = H1 < threshold  # mask = H1==0
        H2 = np.ma.masked_array(H1, mask)
    
>       colormap = mpl.cm.get_cmap(color_map).copy()
E       AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap'

examples/density_plot.py:73: AttributeError
@QuLogic
Copy link
Author

QuLogic commented Apr 29, 2024

You can fix it by using matplotlib.pyplot .get_cmap instead (and while at it, you really should replace import pylab as plt with import matplotlib.pyplot as plt).

@eleftherioszisis
Copy link
Contributor

Thank you for catching that. I merged a fix with your suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants