【问题标题】:How to get names of all colorscales in plotly?如何在情节中获取所有色阶的名称?
【发布时间】:2019-08-19 15:34:43
【问题描述】:

我想知道如何在一个列表中以 plotly 的形式获取所有可用色标的名称。

通缉:


import plotly
import plotly.offline as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
import plotly.tools as tls
from plotly.offline import plot, iplot, init_notebook_mode
init_notebook_mode(connected=False)

colorscales = plotly.something.something

import plotly.graph_objs as go
x = ['Albania', 'Denmark', 'France', 'Finland', 'USSR']
y = [4, 2, 3, 5, 9]
fig = go.FigureWidget(data=[go.Bar(x=x, y=y,
                                 marker={'color': y,
                                               'colorscale': 'Viridis'})]) 
fig

在这里,我想用 colorscales[0] 替换 'Viridis' 并选择它提供的任何颜色图。

【问题讨论】:

标签: python r plotly


【解决方案1】:

您可以在迭代已知颜色模块时使用inspect 模块获取所有单独的色阶名称:

import inspect

colorscale_names = []
colors_modules = ['carto', 'colorbrewer', 'cmocean', 'cyclical',
                    'diverging', 'plotlyjs', 'qualitative', 'sequential']
for color_module in colors_modules:
    colorscale_names.extend([name for name, body
                            in inspect.getmembers(getattr(px.colors, color_module))
                            if isinstance(body, list)])

所有名称现在都在列表colorscale_names 中可用(包括来自PLOTLY_SCALES 的名称,因为它们已在颜色模块中复制)。有一些重复项,您可以通过将其转换为一组来删除它们。

这是列中打印的所有名称。

from textwrap import fill

print(fill(''.join(sorted({f'{x: <{15}}' for x in colorscale_names})), 75))
Accent         Aggrnyl        Agsunset       Alphabet       Antique
Armyrose       Blackbody      Bluered        Blues          Blugrn
Bluyl          Bold           BrBG           Brwnyl         BuGn
BuPu           Burg           Burgyl         Cividis        D3
Dark2          Dark24         Darkmint       Earth          Edge
Electric       Emrld          Fall           G10            Geyser
GnBu           Greens         Greys          HSV            Hot
IceFire        Inferno        Jet            Light24        Magenta
Magma          Mint           OrRd           Oranges        Oryel
PRGn           Paired         Pastel         Pastel1        Pastel2
Peach          Phase          PiYG           Picnic         Pinkyl
Plasma         Plotly         Plotly3        Portland       Prism
PuBu           PuBuGn         PuOr           PuRd           Purp
Purples        Purpor         Rainbow        RdBu           RdGy
RdPu           RdYlBu         RdYlGn         Redor          Reds
Safe           Set1           Set2           Set3           Spectral
Sunset         Sunsetdark     T10            Teal           Tealgrn
Tealrose       Temps          Tropic         Twilight       Viridis
Vivid          YlGn           YlGnBu         YlOrBr         YlOrRd
algae          amp            balance        curl           deep
delta          dense          gray           haline         ice
matter         mrybm          mygbm          oxy            phase
scale_pairs    scale_sequence solar          speed          tempo
thermal        turbid

【讨论】:

    【解决方案2】:

    更新:现在有一个专门的文档页面,包括https://plotly.com/python/builtin-colorscales/的样本


    This file 包含您要查找的大部分内容。

    所以from plotly._plotly_utils.colors import PLOTLY_SCALES 可能会允许您执行colorscales = PLOTLY_SCALES.keys(),尽管这是一个内部对象。

    我应该补充一点,Plotly 可以通过名称识别其他色标,但它们不能作为像这样的简洁导入提供,因为它们在 plotly.express.colors.sequentialplotly.express.colors.cyclical 等其他模块中。

    【讨论】:

    • 请注意,plotly builtin-colorscales 文档页面仅列出了 Express 和 Dash 的色标,但没有列出“go” API 可用的色标。 :-( 例如,在 Plotly Express "px" 中,色阶 "turbo" 存在,但在 Plotly 的 "go" 版本中没有定义。
    • 这实际上是不正确的:Express 和 Graph 对象共享同一组色标。 Turbo 对两者都适用(尽管它是最近才推出的,所以一定要升级到最新版本!)
    • "only 最近" 必须是最近几天。从字面上看,我只是收到错误消息,涡轮未被定义为 go-plotly 的一部分,并且我在三天前 pip-installed plotly。
    • 2020 年 11 月 11 日在 4.13 中添加:github.com/plotly/plotly.py/blob/master/… ... 此代码在 4.14.3 中为我运行:go.Figure(go.Scatter(mode="markers",y=[1,2,3,4],marker_color=[1,2,3,4],marker_colorscale="turbo"))
    【解决方案3】:

    以下都是plotly中的色标

    ['aggrnyl', 'agsunset', 'algae', 'amp', 'armyrose', 'balance',
    'blackbody', 'bluered', 'blues', 'blugrn', 'bluyl', 'brbg',
    'brwnyl', 'bugn', 'bupu', 'burg', 'burgyl', 'cividis', 'curl',
    'darkmint', 'deep', 'delta', 'dense', 'earth', 'edge', 'electric',
    'emrld', 'fall', 'geyser', 'gnbu', 'gray', 'greens', 'greys',
    'haline', 'hot', 'hsv', 'ice', 'icefire', 'inferno', 'jet',
    'magenta', 'magma', 'matter', 'mint', 'mrybm', 'mygbm', 'oranges',
    'orrd', 'oryel', 'peach', 'phase', 'picnic', 'pinkyl', 'piyg',
    'plasma', 'plotly3', 'portland', 'prgn', 'pubu', 'pubugn', 'puor',
    'purd', 'purp', 'purples', 'purpor', 'rainbow', 'rdbu', 'rdgy',
    'rdpu', 'rdylbu', 'rdylgn', 'redor', 'reds', 'solar', 'spectral',
    'speed', 'sunset', 'sunsetdark', 'teal', 'tealgrn', 'tealrose',
    'tempo', 'temps', 'thermal', 'tropic', 'turbid', 'twilight',
    'viridis', 'ylgn', 'ylgnbu', 'ylorbr', 'ylorrd']
    

    【讨论】:

      【解决方案4】:

      我不知道如何在一个列表中获取所有名称,但我通常会在 colorscale 名称中打错字,例如Viridis1 而不是 Viridis,然后查看错误消息并复制可用色标列表。

      这不是最好的方法,但它确实有效。

      【讨论】:

        猜你喜欢
        • 2019-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-08
        • 2020-10-26
        • 1970-01-01
        • 1970-01-01
        • 2016-01-29
        相关资源
        最近更新 更多