📜  如何在 Plotly-Python 中获取所有色阶的名称?

📅  最后修改于: 2022-05-13 01:55:40.761000             🧑  作者: Mango

如何在 Plotly-Python 中获取所有色阶的名称?

在本文中,我们将学习如何在Python的 plotly express 中隐藏颜色条和图例。颜色条是从浅到深或相反的渐变。它们非常适合可视化从低到高的数据集,例如收入、温度或年龄。

逐步实施

第1步:

导入所有需要的包。

Python3
# import the modules
import inspect
import plotly.express as px
from textwrap import fill


Python3
# iterating over color module
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)])


Python3
# import the modules
import inspect
import plotly.express as px
from textwrap import fill
  
# iterating over color module
colorscale_names = []
colors_modules = ['carto', '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)])
  
  
print(fill(''.join(sorted({f'{x: <{15}}' for x in colorscale_names})), 75))


Python3
import plotly.graph_objects as go
import numpy as np
  
fig = go.Figure(data=go.Scatter(
    y=np.random.randn(500),
    mode='markers',
    marker=dict(
        size=8,
        color=np.random.randn(550),  # set color equal to a variable
        colorscale='Agsunset',  # one of plotly colorscales
        showscale=True
    )
))
  
fig.update_layout(
    margin=dict(l=12, r=5, t=20, b=20),
    paper_bgcolor="LightSteelBlue",
)
  
fig.show()


Python3
import plotly.graph_objects as go
import numpy as np
  
fig = go.Figure(data=go.Scatter(
    y=np.random.randn(500),
    mode='markers',
    marker=dict(
        size=8,
        color=np.random.randn(550),  # set color equal to a variable
        colorscale='Agsunset_r',  # reverse Agsunset colorscales
        showscale=True
    )
))
  
fig.update_layout(
    margin=dict(l=12, r=5, t=20, b=20),
    paper_bgcolor="LightSteelBlue",
)
  
  
fig.show()


第2步:

在这里,我们将在迭代颜色模块时使用检查模块获取所有单独的色阶名称。

Python3

# iterating over color module
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)])

完整代码:

Python3

# import the modules
import inspect
import plotly.express as px
from textwrap import fill
  
# iterating over color module
colorscale_names = []
colors_modules = ['carto', '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)])
  
  
print(fill(''.join(sorted({f'{x: <{15}}' for x in colorscale_names})), 75))

输出:

Aggrnyl和 Aggrnyl _r之间的区别在于它将显示相反的比例,即 Aggrnyl(从浅到深) Aggrnyl_r(从深到浅)。在下面的示例中更清楚地理解它。

注意:由于版本控制,某些色阶名称可能不起作用。

Aggrnyl        Aggrnyl_r      Agsunset       Agsunset_r     Alphabet
Alphabet_r     Antique        Antique_r      Armyrose       Armyrose_r
Blackbody      Blackbody_r    Bluered        Bluered_r      Blues
Blues_r        Blugrn         Blugrn_r       Bluyl          Bluyl_r
Bold           Bold_r         BrBG           BrBG_r         Brwnyl
Brwnyl_r       BuGn           BuGn_r         BuPu           BuPu_r
Burg           Burg_r         Burgyl         Burgyl_r       Cividis
Cividis_r      D3             D3_r           Dark2          Dark24
Dark24_r       Dark2_r        Darkmint       Darkmint_r     Earth
Earth_r        Edge           Edge_r         Electric       Electric_r
Emrld          Emrld_r        Fall           Fall_r         G10
G10_r          Geyser         Geyser_r       GnBu           GnBu_r
Greens         Greens_r       Greys          Greys_r        HSV
HSV_r          Hot            Hot_r          IceFire        IceFire_r
Inferno        Inferno_r      Jet            Jet_r          Light24
Light24_r      Magenta        Magenta_r      Magma          Magma_r
Mint           Mint_r         OrRd           OrRd_r         Oranges
Oranges_r      Oryel          Oryel_r        PRGn           PRGn_r
Pastel         Pastel1        Pastel1_r      Pastel2        Pastel2_r
Pastel_r       Peach          Peach_r        Phase          Phase_r
PiYG           PiYG_r         Picnic         Picnic_r       Pinkyl
Pinkyl_r       Plasma         Plasma_r       Plotly         Plotly3
Plotly3_r      Plotly_r       Portland       Portland_r     Prism
Prism_r        PuBu           PuBuGn         PuBuGn_r       PuBu_r
PuOr           PuOr_r         PuRd           PuRd_r         Purp
Purp_r         Purples        Purples_r      Purpor         Purpor_r
Rainbow        Rainbow_r      RdBu           RdBu_r         RdGy
RdGy_r         RdPu           RdPu_r         RdYlBu         RdYlBu_r
RdYlGn         RdYlGn_r       Redor          Redor_r        Reds
Reds_r         Safe           Safe_r         Set1           Set1_r
Set2           Set2_r         Set3           Set3_r         Spectral
Spectral_r     Sunset         Sunset_r       Sunsetdark     Sunsetdark_r
T10            T10_r          Teal           Teal_r         Tealgrn
Tealgrn_r      Tealrose       Tealrose_r     Temps          Temps_r
Tropic         Tropic_r       Turbo          Turbo_r        Twilight
Twilight_r     Viridis        Viridis_r      Vivid          Vivid_r
YlGn           YlGnBu         YlGnBu_r       YlGn_r         YlOrBr
YlOrBr_r       YlOrRd         YlOrRd_r       __all__        _cols
algae          algae_r        amp            amp_r          balance
balance_r      curl           curl_r         deep           deep_r
delta          delta_r        dense          dense_r        gray
gray_r         haline         haline_r       ice            ice_r
matter         matter_r       mrybm          mrybm_r        mygbm
mygbm_r        oxy            oxy_r          phase          phase_r
scale_pairs    scale_pairs_r  scale_sequence scale_sequence_rsolar
solar_r        speed          speed_r        tempo          tempo_r
thermal        thermal_r      turbid         turbid_r

示例 1:

在这个例子中,我们在 Plotly Express 中选择 color-scale 作为colorscale = “Agsunset” ,这将从内置的 plotly 库中选择 Aggrnyl colorscale。

Python3

import plotly.graph_objects as go
import numpy as np
  
fig = go.Figure(data=go.Scatter(
    y=np.random.randn(500),
    mode='markers',
    marker=dict(
        size=8,
        color=np.random.randn(550),  # set color equal to a variable
        colorscale='Agsunset',  # one of plotly colorscales
        showscale=True
    )
))
  
fig.update_layout(
    margin=dict(l=12, r=5, t=20, b=20),
    paper_bgcolor="LightSteelBlue",
)
  
fig.show()

输出:

示例 2:

在这个例子中,我们在 Plotly Express 中选择 color-scale 为colorscale = “Agsunset_r”( r 代表 reverse ,这将从内置的 plotly 库中选择reverse Aggrnyl_r colorscale。两者之间的唯一区别是它将显示相反的比例,即 Aggrnyl_r(从暗到亮)和 Aggrnyl(从亮到暗)。

Python3

import plotly.graph_objects as go
import numpy as np
  
fig = go.Figure(data=go.Scatter(
    y=np.random.randn(500),
    mode='markers',
    marker=dict(
        size=8,
        color=np.random.randn(550),  # set color equal to a variable
        colorscale='Agsunset_r',  # reverse Agsunset colorscales
        showscale=True
    )
))
  
fig.update_layout(
    margin=dict(l=12, r=5, t=20, b=20),
    paper_bgcolor="LightSteelBlue",
)
  
  
fig.show()

输出: