【问题标题】:matplotlib color gradient between two colorsmatplotlib 两种颜色之间的颜色渐变
【发布时间】:2019-12-07 16:00:06
【问题描述】:

我想要 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()

要获得这样的颜色渐变,我需要进行哪些更改?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您可以从 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()
    

    这会导致:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-27
      • 2012-11-27
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 2014-09-14
      相关资源
      最近更新 更多