Applying a normalized scalar colormap to a matplotlib plot, still getting default colors

Mike

I'm trying to get a plot to have a different colormap than the default. The dark2 colormap looks like the best thing for my data, but it's all coming out with the default colors. What am I missing?

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pylab import get_cmap

data = [{'a': 1, 'b': 1, 'c': 2, 'd': 0},{'a': 1, 'b': 1, 'c': 2, 'd': 0},
        {'a': 1, 'b': 1, 'c': 1, 'd': 2},{'a': 1, 'b': 2.5, 'c': 1, 'd': 2},
        {'a': 1.7, 'b': 2.1, 'c': 2, 'd': 0.2},{'a': 1.7, 'b': 2.1, 'c': 2, 'd': 0.2}]
cm = get_cmap('Dark2')

cnorm = matplotlib.colors.Normalize(vmin=0, vmax=len(data))
smap = matplotlib.cm.ScalarMappable(norm=cnorm, cmap=cm)
_fig, ax = plt.subplots(1, 1, figsize=(18, 8))
for lx in range(len(ax.axes.lines)):
    ax.axes.lines[lx].set_color(smap.to_rgba(lx))
    print('setting color for %i to %s' % (lx, smap.to_rgba(lx)))

frm = pd.DataFrame(data)
frm.plot(ax=ax)

plt.show()
Paul H

You're setting the colors on an empty list. Just move the code around and you're set:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

%matplotlib inline

data = [
    {'a': 1, 'b': 1, 'c': 2, 'd': 0},
    {'a': 1, 'b': 1, 'c': 2, 'd': 0},
    {'a': 1, 'b': 1, 'c': 1, 'd': 2},
    {'a': 1, 'b': 2.5, 'c': 1, 'd': 2},
    {'a': 1.7, 'b': 2.1, 'c': 2, 'd': 0.2},
    {'a': 1.7, 'b': 2.1, 'c': 2, 'd': 0.2}
]
cm = matplotlib.cm.Dark2

cnorm = matplotlib.colors.Normalize(vmin=0, vmax=len(data))
smap = matplotlib.cm.ScalarMappable(norm=cnorm, cmap=cm)
_fig, ax = plt.subplots(1, 1, figsize=(18, 8))

frm = pd.DataFrame(data)
frm.plot(ax=ax, legend=False)  # legend=False b/c we haven't tweaked colors yet

for lx in range(len(ax.axes.lines)):
    ax.axes.lines[lx].set_color(smap.to_rgba(lx))

    # now this will actually print something
    print('setting color for %i to %s' % (lx, smap.to_rgba(lx)))

ax.legend()  # and now do the legend

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Applying a normalized scalar colormap to a matplotlib plot, still getting default colors

From Dev

Matplotlib: Custom colormap with three colors

From Dev

How to set default colormap in Matplotlib

From Dev

Plot histogram with colors taken from colormap

From Dev

How to determine colors at intervals in a matplotlib colormap

From Dev

Matplotlib 3D plot use colormap

From Dev

matplotlib errorbar plot - using a custom colormap

From Dev

Pandas DataFrame Plot: Permanently change default colormap

From Dev

GeoPandas, MatPlotLib Plot Custom Colors

From Dev

Exclude specific color/colors from colormap in groupby plot

From Dev

pandas matplot retrieve colors used by colormap stacked bar plot

From Dev

Associating colors from a continuous colormap to specific values in matplotlib

From Dev

Do matplotlib.contourf levels depend on the amount of colors in the colormap?

From Dev

Matplotlib scatter plot different colors in legend and plot

From Dev

Matplotlib scatter plot different colors in legend and plot

From Dev

Matplotlib create surface plot (x,y,z,color) given csv data - getting wrong colors

From Dev

How to plot log normalized image using imshow () (matplotlib)?

From Dev

APLpy show markers normalized by a colormap

From Java

Create own colormap using matplotlib and plot color scale

From Dev

How to set marker type for a specific point in a matplotlib scatter plot with colormap

From Dev

Python - colormap in matplotlib for 3D line plot

From Dev

Plot a (polar) color wheel based on a colormap using Python/Matplotlib

From Dev

Custom colormap in matplotlib for 3D surface plot

From Dev

Matplotlib: How to plot a small rectangle filled with a colormap as a legend

From Dev

Python - colormap in matplotlib for 3D line plot

From Dev

Matplotlib's colormap is not being applied to scatter plot, no error is returned

From Dev

matplotlib scatter plot with different markers and colors

From Dev

Reversing colormaps or specifying colors in a matplotlib/pandas plot

From Dev

python matplotlib: retrieving colors used in contour plot

Related Related

  1. 1

    Applying a normalized scalar colormap to a matplotlib plot, still getting default colors

  2. 2

    Matplotlib: Custom colormap with three colors

  3. 3

    How to set default colormap in Matplotlib

  4. 4

    Plot histogram with colors taken from colormap

  5. 5

    How to determine colors at intervals in a matplotlib colormap

  6. 6

    Matplotlib 3D plot use colormap

  7. 7

    matplotlib errorbar plot - using a custom colormap

  8. 8

    Pandas DataFrame Plot: Permanently change default colormap

  9. 9

    GeoPandas, MatPlotLib Plot Custom Colors

  10. 10

    Exclude specific color/colors from colormap in groupby plot

  11. 11

    pandas matplot retrieve colors used by colormap stacked bar plot

  12. 12

    Associating colors from a continuous colormap to specific values in matplotlib

  13. 13

    Do matplotlib.contourf levels depend on the amount of colors in the colormap?

  14. 14

    Matplotlib scatter plot different colors in legend and plot

  15. 15

    Matplotlib scatter plot different colors in legend and plot

  16. 16

    Matplotlib create surface plot (x,y,z,color) given csv data - getting wrong colors

  17. 17

    How to plot log normalized image using imshow () (matplotlib)?

  18. 18

    APLpy show markers normalized by a colormap

  19. 19

    Create own colormap using matplotlib and plot color scale

  20. 20

    How to set marker type for a specific point in a matplotlib scatter plot with colormap

  21. 21

    Python - colormap in matplotlib for 3D line plot

  22. 22

    Plot a (polar) color wheel based on a colormap using Python/Matplotlib

  23. 23

    Custom colormap in matplotlib for 3D surface plot

  24. 24

    Matplotlib: How to plot a small rectangle filled with a colormap as a legend

  25. 25

    Python - colormap in matplotlib for 3D line plot

  26. 26

    Matplotlib's colormap is not being applied to scatter plot, no error is returned

  27. 27

    matplotlib scatter plot with different markers and colors

  28. 28

    Reversing colormaps or specifying colors in a matplotlib/pandas plot

  29. 29

    python matplotlib: retrieving colors used in contour plot

HotTag

Archive