📜  如何在Python中的 Plotly 中更改颜色条

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

如何在Python中的 Plotly 中更改颜色条

在本文中,我们将学习如何在 Plotly Python中更改颜色条。

Plotly 的不同类型的色阶名称

aggrnyl     burginferno     plasma      rdpu        ylgnbu      mattergeyser
agsunset    burgyl      jet         plotly3redorylorbr      solarpiyg
blackbodycividismagenta     pubu        reds        ylorrd      speedpicnic
blues       darkmintmagmapubugn      sunsetalgae       tempoportland
bluered     electricmintpurd        sunsetdark  amp         thermalpuor
blugrn      emrldorrdpurptealdeepturbidrdgy
bluyl       gnbuorangespurplestealgrn     dense       armyroserdylbu
brwnylgreensoryel       purporturbogray        brbgrdylgn
bugn        greyspeachrainbow     viridishaline      earthspectral
bupu        hotpinkylrdbuylgn        ice         falltealrose
tempstropicbalancecurldeltaoxyedgehsv
icefirephasetwilightmrybmmygbm   

句法:

我们可以通过使用色标来改变颜色。

fig = go.Figure(data=go.Scatter(
    y=np.random.randn(500),
    mode='markers',
    marker=dict(
        size=8,
        
        # set color equal to a variable
        color=np.random.randn(500),
        
        # one of plotly colorscales
        colorscale='hot',
        
        # enable color scale
        showscale=True
    )
))

示例 1:

Python3
# import the modules
import plotly.graph_objects as go
import numpy as np
  
# create figure
# from the data using numpy random method
fig = go.Figure(data=go.Scatter(
    y=np.random.randn(500),
    mode='markers',
    marker=dict(
        size=8,
        
        # set color equal to a variable
        color=np.random.randn(500),
          
        # one of plotly colorscales
        colorscale='hot',
          
        # enable color scale
        showscale=True
    )
))
  
# display figure
fig.show()


Python3
import plotly.graph_objects as go
import numpy as np
  
fig = go.Figure(data=go.Line(
    y = np.random.randn(500),
    mode='markers',
    marker=dict(
        size=8,
        color=np.random.randn(500), #set color equal to a variable
        colorscale='hot_r', # one of plotly colorscales
        showscale=True  # enable color scale
    )
))
  
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,
        
        # set color equal to a variable
        color=np.random.randn(550),
          
        # one of plotly colorscales
        colorscale='turbo_r',
          
        # enable color scale
        showscale=True
    )
))
# display
fig.show()


输出:

示例 2:

将颜色设置为 hot_r

Python3

import plotly.graph_objects as go
import numpy as np
  
fig = go.Figure(data=go.Line(
    y = np.random.randn(500),
    mode='markers',
    marker=dict(
        size=8,
        color=np.random.randn(500), #set color equal to a variable
        colorscale='hot_r', # one of plotly colorscales
        showscale=True  # enable color scale
    )
))
  
fig.show()

输出:

示例 3:

将颜色设置为 turbo_r

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,
        
        # set color equal to a variable
        color=np.random.randn(550),
          
        # one of plotly colorscales
        colorscale='turbo_r',
          
        # enable color scale
        showscale=True
    )
))
# display
fig.show()

输出: