如何反转 Plotly 的颜色条的方向,以便顶部的小值和底部的大值

问题描述 投票:0回答:1

我目前正在使用

plotly
上的颜色条来指示放置水下声纳接收器的深度。目前,颜色条看起来像这样:

但是,我认为如果较大的值(表示较大的深度)应位于底部,而较小的数字(表示较浅的深度(即设备已放置在靠近表面的位置)应位于顶部,则更有意义, 但不知道该怎么做。

我用来创建图形和颜色条的代码由两个字典组成(一个指定数据,另一个指定布局)。

import plotly.offline as off
import _tkinter

from matplotlib import pyplot as plt
from matplotlib import ticker
from matplotlib.dates import drange

...

data = [ 
   dict(
        lat = lat_array,
        lon = lon_array,
        marker = dict(
            color = log_depth_array,
            size = 6,
            colorbar = dict(
                title = 'Log Depth',
                thickness = 10,
                titleside = "right",
                outlinecolor = "rgba(68, 68, 68, 0)",
                ticks = "outside",
                ticklen = 3,
                showticksuffix = "last",
                ticksuffix = " log(meters, 10)",
                dtick = .1
            ),
        ),
        mode = 'markers',
        text = mouseover_text,
        type = 'scattergeo'
    ) ]

layout = dict(
    geo = dict(
        showframe = True,
        framewidth = 25,
        scope = 'north america',
        showland = True,
        landcolor = "rgb(212, 212, 212)",
        showocean = True,
        oceancolor = "rgb(200, 255, 255)",
        subunitcolor = "rgb(0,0,0)",
        resolution = 50,
        projection = dict(
            type = 'robinson',
            rotation = dict(
                lon = -100
            )
        ),
        lonaxis = dict(
            showgrid = True,
            gridwidth = 0.5,
            range= [ lon_min-.4, lon_max+.4 ],
            dtick = 5
        ),
        lataxis = dict (
            showgrid = True,
            gridwidth = 0.5,
            range= [ lat_min-.4, lat_max+.4 ],
            dtick = 5
        )
    ),
)
fig = { 'data':data, 'layout':layout }
off.iplot(fig)

我应该添加什么参数(可能添加到数据字典中的颜色条字典)以使颜色条底部的数字代表更大的深度?

python plotly plotly-python
1个回答
0
投票

您可以反转色标,然后通过传递

tickvals = [1, 1.1, ... 3.4]
硬编码刻度值,并使刻度文本相反:
ticktext = ['3.4', '3.3', ... '1']
。这还需要您手动将文本
" log(meters, 10)"
添加到最上面的刻度。

我不确定为什么,但是有一种奇怪的行为,tickvals 和 ticktext 在顶部和底部被截断——您可能需要调整一些 tick 的起始值和结束值。它可能与颜色条的默认填充有关,或者我可能错过了一些东西。

import plotly.offline as off
import _tkinter

from matplotlib import pyplot as plt
from matplotlib import ticker
from matplotlib.dates import drange

import numpy as np
import pandas as pd

# use some sample data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
lat_array, lon_array = df['lat'], df['long']
lat_min, lon_min = min(lat_array), min(lon_array)
lat_max, lon_max = max(lat_array), max(lon_array)
np.random.seed(42)
log_depth_array = np.random.uniform(0,3.3,size=len(df))

tickvals = np.arange(1-0.2,3.5,0.1)
ticktext = [str(f"{val:.1f}") for val in tickvals[::-1]]
ticktext[-3] = ticktext[-3] + " log(meters, 10)"

data = [ 
   dict(
        lat = lat_array,
        lon = lon_array,
        marker = dict(
            color = log_depth_array,
            colorscale = 'viridis_r',
            size = 6,
            colorbar = dict(
                title = 'Log Depth',
                # colorscale = "viridis_r",
                thickness = 10,
                titleside = "right",
                outlinecolor = "rgba(68, 68, 68, 0)",
                ticks = "outside",
                tickmode = "array",
                tickvals = tickvals,
                ticktext = ticktext,
                ticklen = 3,
            ),
        ),
        mode = 'markers',
        # text = mouseover_text,
        type = 'scattergeo'
    ) ]

layout = dict(
    geo = dict(
        showframe = True,
        framewidth = 25,
        scope = 'north america',
        showland = True,
        landcolor = "rgb(212, 212, 212)",
        showocean = True,
        oceancolor = "rgb(200, 255, 255)",
        subunitcolor = "rgb(0,0,0)",
        resolution = 50,
        projection = dict(
            type = 'robinson',
            rotation = dict(
                lon = -100
            )
        ),
        lonaxis = dict(
            showgrid = True,
            gridwidth = 0,
            range= [ lon_min-.4, lon_max+.4 ],
            dtick = 5
        ),
        lataxis = dict (
            showgrid = True,
            gridwidth = 0,
            range= [ lat_min-.4, lat_max+.4 ],
            dtick = 5
        )
    ),
)
fig = { 'data':data, 'layout':layout }
off.iplot(fig)

© www.soinside.com 2019 - 2024. All rights reserved.