Using a colormap for matplotlib line plots
Asked Answered
B

2

17

I’d like to employ the reverse Spectral colormap ,

https://matplotlib.org/examples/color/colormaps_reference.html

for a lineplot.

This works fine with a hex bin plot::

color_map = plt.cm.Spectral_r
image = plt.hexbin(x,y,cmap=color_map)

but when I do

ax1.plot(x,y, cmp=color_map)

this gives me::

AttributeError: Unknown property cmap

Note, I just want to set the colormap and let matplotliob do the rest; i.e. I don't want to have a color=' argument in the .plot command.

Bismarck answered 19/9, 2017 at 9:14 Comment(6)
Unfortunately it is unclear what the desired output of something like ax.plot(x,y, cmap=color_map) should be. plot does not have a cmap argument; most certainly because it is not clear what quantity should be colormapped in a line plot. If you can tell what the desired output should be, there might be a goodalternative.Elswick
Why the heck did this get downvoted??!!Bismarck
While I did not downvote, the reason other people did is most certainly explained by my comment. You should update your question as to include the desired behaviour because as it stands it is not clear what you are expecting to happen when using the (non-existant) cmap argument.Elswick
I still don't understand why this is getting down-voted!! I was just keen to employ a nice colormap scheme to my line plots!! Eeeks!!!Bismarck
You still haven't said what you expect.Elswick
Well, what I expected is that by default the line would be colormapped according t the y-axis, and there would be an option to color the lines by the x-axis...I'm still looking for how to color a lineplot based on x-axis values.Burgrave
M
12

You can have a look at this solution - the third variant is what you want:

https://mcmap.net/q/270870/-use-matplotlib-color-map-for-color-cycle

You need to know how many lines you're plotting in advance, as otherwise it doesn't know how to choose the colours from the range.

Mcalpine answered 19/9, 2017 at 10:21 Comment(0)
C
6

I think that seaborn's color_palette function is very convenient for this purpose. It can be used in a with statement to temporarily set the color cycle for a plot or set of plots. For example:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

with sns.color_palette("Spectral", n_colors=10):
    plt.plot(np.random.rand(5, 10))

You can use with any predefined matplotlib or seaborn colormap, or provide a custom sequence of colors.

Contrastive answered 18/11, 2020 at 17:20 Comment(6)
While this is convenient, it seems to affect also subsequent lines added to the same plot outside of the with clause.Kiaochow
It's a very game changing way to set colours. Thanks @Contrastive !Kushner
unfortunately it does not seem to work for a plt.scatter()Arica
it does not work in subplots ?Arica
@Arica For plt.scatter plots you can use the c and/or cmap keyword arguments. What do you mean with “it does not work in subplots”? Using different color maps in different subplots?Contrastive
It seems to me that it does not work if I add a plt.subplot(), even without color change between subplots.Arica

© 2022 - 2025 — McMap. All rights reserved.