从绘图色标访问颜色

JP-*_*lis 7 python plotly-python

Plotly 有没有办法访问其范围内任何值的颜色图颜色?

我知道我可以从

plotly.colors.PLOTLY_SCALES["Viridis"]
Run Code Online (Sandbox Code Playgroud)

但我无法找到如何访问中间/插值。

此问题中显示了 Matplotlib 中的等效项。还有另一个问题解决了colorlover图书馆的类似问题,但都没有提供很好的解决方案。

mat*_*ias 15

有一个内置方法plotly.express.colors可以sample_colorscale提供颜色样本:

from plotly.express.colors import sample_colorscale

import plotly.graph_objects as go
import numpy as np

x = np.linspace(0, 1, 25)
c = sample_colorscale('jet', list(x))

fig = go.FigureWidget()
fig.add_trace(
    go.Bar(x=x, y=y, marker_color=c)
)
fig.show()
Run Code Online (Sandbox Code Playgroud)

查看输出图-> sampled_colors


Ada*_*dam 8

Plotly 好像没有这样的方法,所以我写了一个:

import plotly.colors

def get_continuous_color(colorscale, intermed):
    """
    Plotly continuous colorscales assign colors to the range [0, 1]. This function computes the intermediate
    color for any value in that range.

    Plotly doesn't make the colorscales directly accessible in a common format.
    Some are ready to use:
    
        colorscale = plotly.colors.PLOTLY_SCALES["Greens"]

    Others are just swatches that need to be constructed into a colorscale:

        viridis_colors, scale = plotly.colors.convert_colors_to_same_type(plotly.colors.sequential.Viridis)
        colorscale = plotly.colors.make_colorscale(viridis_colors, scale=scale)

    :param colorscale: A plotly continuous colorscale defined with RGB string colors.
    :param intermed: value in the range [0, 1]
    :return: color in rgb string format
    :rtype: str
    """
    if len(colorscale) < 1:
        raise ValueError("colorscale must have at least one color")

    if intermed <= 0 or len(colorscale) == 1:
        return colorscale[0][1]
    if intermed >= 1:
        return colorscale[-1][1]

    for cutoff, color in colorscale:
        if intermed > cutoff:
            low_cutoff, low_color = cutoff, color
        else:
            high_cutoff, high_color = cutoff, color
            break

    # noinspection PyUnboundLocalVariable
    return plotly.colors.find_intermediate_color(
        lowcolor=low_color, highcolor=high_color,
        intermed=((intermed - low_cutoff) / (high_cutoff - low_cutoff)),
        colortype="rgb")
Run Code Online (Sandbox Code Playgroud)

挑战在于内置的 Plotly 色阶不是始终暴露的。有些已经定义为色标,而其他的只是必须首先转换为色标的色样列表。

Viridis 色阶是用十六进制值定义的,而 Plotly 颜色操作方法不喜欢这种值,因此最容易从这样的色板构建它:

viridis_colors, _ = plotly.colors.convert_colors_to_same_type(plotly.colors.sequential.Viridis)
colorscale = plotly.colors.make_colorscale(viridis_colors)

get_continuous_color(colorscale, intermed=0.25)
# rgb(58.75, 80.75, 138.25)
Run Code Online (Sandbox Code Playgroud)


Dav*_*_sd 7

这个答案扩展了 Adam 提供的已经很好的答案。特别是,它处理 Plotly 色标的不一致问题。

在 Plotly 中,您可以通过编写 来指定内置色标colorscale="name_of_the_colorscale"。这表明 Plotly 已经有一个内置工具,可以以某种方式将色标转换为适当的值,并且能够处理这些不一致的情况。通过搜索 Plotly 的源代码我们找到了有用的ColorscaleValidator类。让我们看看如何使用它:

def get_color(colorscale_name, loc):
    from _plotly_utils.basevalidators import ColorscaleValidator
    # first parameter: Name of the property being validated
    # second parameter: a string, doesn't really matter in our use case
    cv = ColorscaleValidator("colorscale", "")
    # colorscale will be a list of lists: [[loc1, "rgb1"], [loc2, "rgb2"], ...] 
    colorscale = cv.validate_coerce(colorscale_name)
    
    if hasattr(loc, "__iter__"):
        return [get_continuous_color(colorscale, x) for x in loc]
    return get_continuous_color(colorscale, loc)
        

# Identical to Adam's answer
import plotly.colors
from PIL import ImageColor

def get_continuous_color(colorscale, intermed):
    """
    Plotly continuous colorscales assign colors to the range [0, 1]. This function computes the intermediate
    color for any value in that range.

    Plotly doesn't make the colorscales directly accessible in a common format.
    Some are ready to use:
    
        colorscale = plotly.colors.PLOTLY_SCALES["Greens"]

    Others are just swatches that need to be constructed into a colorscale:

        viridis_colors, scale = plotly.colors.convert_colors_to_same_type(plotly.colors.sequential.Viridis)
        colorscale = plotly.colors.make_colorscale(viridis_colors, scale=scale)

    :param colorscale: A plotly continuous colorscale defined with RGB string colors.
    :param intermed: value in the range [0, 1]
    :return: color in rgb string format
    :rtype: str
    """
    if len(colorscale) < 1:
        raise ValueError("colorscale must have at least one color")

    hex_to_rgb = lambda c: "rgb" + str(ImageColor.getcolor(c, "RGB"))

    if intermed <= 0 or len(colorscale) == 1:
        c = colorscale[0][1]
        return c if c[0] != "#" else hex_to_rgb(c)
    if intermed >= 1:
        c = colorscale[-1][1]
        return c if c[0] != "#" else hex_to_rgb(c)

    for cutoff, color in colorscale:
        if intermed > cutoff:
            low_cutoff, low_color = cutoff, color
        else:
            high_cutoff, high_color = cutoff, color
            break

    if (low_color[0] == "#") or (high_color[0] == "#"):
        # some color scale names (such as cividis) returns:
        # [[loc1, "hex1"], [loc2, "hex2"], ...]
        low_color = hex_to_rgb(low_color)
        high_color = hex_to_rgb(high_color)

    return plotly.colors.find_intermediate_color(
        lowcolor=low_color,
        highcolor=high_color,
        intermed=((intermed - low_cutoff) / (high_cutoff - low_cutoff)),
        colortype="rgb",
    )
Run Code Online (Sandbox Code Playgroud)

此时,您所要做的就是:

get_color("phase", 0.5)
# 'rgb(123.99999999999999, 112.00000000000001, 236.0)'

import numpy as np
get_color("phase", np.linspace(0, 1, 256))
# ['rgb(167, 119, 12)',
#  'rgb(168.2941176470588, 118.0078431372549, 13.68235294117647)',
#  ...
Run Code Online (Sandbox Code Playgroud)

编辑:改进处理特殊情况。