简体   繁体   中英

Plotly colorscale reverse direction

How can I reverse the direction of a colorscale in plotly? I am trying to use the color scale Viridis for a marker plot

import plotly.graph_objs as go
import plotly.plotly as py

import numpy as np

trace1 = go.Scatter(
    y = np.random.randn(500),
    mode='markers',
    marker=dict(
        size=16,
        color = np.random.randn(500), #set color equal to a variable
        colorscale='Viridis',
        showscale=True
    )
)
data = [trace1]

py.iplot(data, filename='scatter-plot-with-colorscale')

but I want it to be darker for higher values and lighter for lower values. Is there any way to do this without defining my own custom colorscale or changing my color parameter array?

Just add reversescale = True to your plot definition and the colorscale is reversed.

import plotly
plotly.offline.init_notebook_mode()
trace = plotly.graph_objs.Heatmap(z=[[1, 20, 30],
                                     [20, 1, 60],
                                     [30, 60, 1]],
                                  reversescale=True)

plotly.offline.iplot([trace])

在此处输入图片说明

For anyone else looking for this like me: contrary to Sheldores comment to the OP, for me adding '_r' to the colorscale name as described here worked perfectly fine. However, I think this was probably changed in plotly since then. Compare the two following figures:

import plotly.graph_objs as go
import plotly.plotly as py
import numpy as np

y_data = np.random.randn(500)
color_data = np.random.randn(500)


fig1 = go.Figure(go.Scatter(
    y = y_data,
    mode='markers',
    marker=dict(
        size=16,
        color = color_data, #set color equal to a variable
        colorscale='Viridis',
        showscale=True
    )
))

fig2 = go.Figure(go.Scatter(
    y = y_data,
    mode='markers',
    marker=dict(
        size=16,
        color = color_data, #set color equal to a variable
        colorscale='Viridis_r',
        showscale=True
    )
))

fig1.show()
fig2.show()

My plotly version is 4.11.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM