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.
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.
You can have a look at the top answer here - the third variant is what you want:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With