python - 如何在 plotly 中获取所有色标的名称?

标签 python r 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' 并选择它提供的任何颜色图。

最佳答案

您可以在遍历已知颜色模块时使用 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

关于python - 如何在 plotly 中获取所有色标的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57559856/

相关文章:

r - 将 ggplotly 和 ggplot 与拼凑而成?

python - lambda 函数中元组的含义

javascript - 如何从 JavaScript 网站抓取数据?

r - 使用多个 geom_point 图层指定指南图例美学

r - choroplethr 图例未打印某些图,scale_fill_brewer() 的问题

r - 在r/formattable中,如何调整列格式不包括 `total`行

r - 为 R : Remove the k that apears on the y axis when the dataset contains numbers larger than 1000 作图

r - 在 plotly R 中更改标题位置

python - 匹配nltk语法中的字母数字字符串

python - 让 Pip 在 Windows 上使用 MinGW32?