我想知道如何在列表中绘制所有可用色标的名称。
通缉:
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/