简体   繁体   English

matplotlib两种颜色之间的颜色渐变

[英]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. 我想要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()

What do I have to change to get such a color gradient? 为了获得这样的颜色渐变,我必须更改什么?

From the matplotlib documentation you can check this link as an example. 您可以从matplotlib文档中查看此链接作为示例。

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: 结果是:

自定义颜色图,从黑色到红色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM