Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib color gradient between two colors

I want a color gradient between black and red in matplotlib, where the low values are black and become more and more red with increasing Y-value.

import matplotlib.pyplot as plt
xvals = np.arange(0, 1, 0.01)
yvals = xvals
plt.plot(xvals, yvals, "r")
axes = plt.axes()
plt.show()

What do I have to change to get such a color gradient?

like image 265
Emma Avatar asked Jul 30 '19 09:07

Emma


Video Answer


1 Answers

From the matplotlib documentation you can check this link as an example.

To create that colormap you just need to do:

import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np

colors = [(0, 0, 0), (1, 0, 0)] # first color is black, last is red
cm = LinearSegmentedColormap.from_list(
        "Custom", colors, N=20)
mat = np.indices((10,10))[1]
plt.imshow(mat, cmap=cm)
plt.show()

This results in this:

Custom colormap from black to red

like image 81
armatita Avatar answered Sep 19 '22 01:09

armatita