我想要matplotlib中黑色和红色之间的颜色渐变,其中低值是黑色,并且随着Y值的增加而变得越来越红。
import matplotlib.pyplot as plt
xvals = np.arange(0, 1, 0.01)
yvals = xvals
plt.plot(xvals, yvals, "r")
axes = plt.axes()
plt.show()
要获得这样的颜色渐变,我必须更改什么?
答案 0 :(得分:5)
从matplotlib文档中,您可以查看this link作为示例。
要创建该颜色图,您只需要做:
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()
结果如下: