我想要 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()
我必须改变什么才能获得这样的颜色渐变?
最佳答案
您可以从 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()
这导致:
关于python - 两种颜色之间的 matplotlib 颜色渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57268627/