【问题标题】:Matplotlib color point with rgba-value Python :O具有 rgba 值 Python 的 Matplotlib 色点:O
【发布时间】:2015-11-26 09:29:55
【问题描述】:

我已经有了一些数据图,我使用contourfcm.jet 作为我的颜色图。 vmin=-100 和 vmax= 100

我在另一个问题中读到,我可以使用 cm.jet(x) 从我的颜色图中访问特定颜色,其中 x 在 [0,1] 中。

我想获得 47 的颜色,所以我想尝试一下

 fortyseven=pylab.cm.jet(0.735) 

这将返回 47 的 rgb 值

然后我想用这个特定的 rgb 值绘制一个点:

ax.plot( x, y, fortyseven) 

但这行不通。我总是得到一个

ValueError: third arg must be a format string

错误。

所以我尝试了

 ax.plot( x, y, 'fortyseven') 

我得到一个

ValueError: Unrecognized character f in format string

错误。

那么甚至可以用 rgb 值为点着色吗?

还是我忽略了什么?

编辑

如果我将 rgb-value 转换为 hex-value 会起作用吗?

编辑

我刚刚意识到它会返回一个rgba-value

【问题讨论】:

    标签: python matplotlib plot colors rgb


    【解决方案1】:

    您也可以在您的plot 命令中使用color 关键字,它接受任何matplotlib 颜色,包括RGBA tuple,这是cm.jet(0.735) 返回的内容。

    ax.plot(x, y, color = fortyseven)
    

    【讨论】:

    • 完美这正是我想知道的。谢谢!
    【解决方案2】:

    好的,我做了一些研究,现在可以回答我自己的问题了。

    实际上 rgba 中的 alpha 代表透明度,因此它不会影响颜色本身,只会影响颜色的不透明程度。

    所以基本上你可以去掉 alpha 值,它不会改变颜色本身。

    甚至还有一个 Python 函数 to_rgb 可以做到这一点。

    说明:

    to_rgb(arg)
    
    Returns an RGB tuple of three floats from 0-1.
    
    arg can be an RGB or RGBA sequence or a string in any of several forms:
    
       a letter from the set ‘rgbcmykw’
       a hex color string, like ‘#00FFFF’
       a standard name, like ‘aqua’
       a string representation of a float, like ‘0.4’, indicating gray on a 0-1 scale
    
    if arg is RGBA, the A will simply be discarded.
    

    所以现在我可以使用 rgb-value 并绘制我的观点。

    【讨论】:

      猜你喜欢
      • 2013-03-11
      • 2015-03-13
      • 2018-02-16
      • 1970-01-01
      • 2015-04-08
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多