如何反转 Plotly 颜色条的方向,使小值在顶部,大值在底部

use*_*262 5 python plotly plotly-python

我目前正在使用颜色条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)
Run Code Online (Sandbox Code Playgroud)

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

Der*_*k O 2

您可以反转色标,然后通过传递对刻度值进行硬编码tickvals = [1, 1.1, ... 3.4],并使刻度文本相反:ticktext = [\'3.4\', \'3.3\', ... \'1\']。这还需要您手动将文本添加" log(meters, 10)"到最上面的刻度线。

\n

我不知道为什么,但有一种奇怪的行为,即刻度值和刻度文本在顶部和底部被切断 \xe2\x80\x93 您可能需要调整一些刻度开始和结束值。这可能与颜色条的默认填充有关,或者也许我错过了一些东西。

\n
import plotly.offline as off\nimport _tkinter\n\nfrom matplotlib import pyplot as plt\nfrom matplotlib import ticker\nfrom matplotlib.dates import drange\n\nimport numpy as np\nimport pandas as pd\n\n# use some sample data\ndf = pd.read_csv(\'https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv\')\nlat_array, lon_array = df[\'lat\'], df[\'long\']\nlat_min, lon_min = min(lat_array), min(lon_array)\nlat_max, lon_max = max(lat_array), max(lon_array)\nnp.random.seed(42)\nlog_depth_array = np.random.uniform(0,3.3,size=len(df))\n\ntickvals = np.arange(1-0.2,3.5,0.1)\nticktext = [str(f"{val:.1f}") for val in tickvals[::-1]]\nticktext[-3] = ticktext[-3] + " log(meters, 10)"\n\ndata = [ \n   dict(\n        lat = lat_array,\n        lon = lon_array,\n        marker = dict(\n            color = log_depth_array,\n            colorscale = \'viridis_r\',\n            size = 6,\n            colorbar = dict(\n                title = \'Log Depth\',\n                # colorscale = "viridis_r",\n                thickness = 10,\n                titleside = "right",\n                outlinecolor = "rgba(68, 68, 68, 0)",\n                ticks = "outside",\n                tickmode = "array",\n                tickvals = tickvals,\n                ticktext = ticktext,\n                ticklen = 3,\n            ),\n        ),\n        mode = \'markers\',\n        # text = mouseover_text,\n        type = \'scattergeo\'\n    ) ]\n\nlayout = dict(\n    geo = dict(\n        showframe = True,\n        framewidth = 25,\n        scope = \'north america\',\n        showland = True,\n        landcolor = "rgb(212, 212, 212)",\n        showocean = True,\n        oceancolor = "rgb(200, 255, 255)",\n        subunitcolor = "rgb(0,0,0)",\n        resolution = 50,\n        projection = dict(\n            type = \'robinson\',\n            rotation = dict(\n                lon = -100\n            )\n        ),\n        lonaxis = dict(\n            showgrid = True,\n            gridwidth = 0,\n            range= [ lon_min-.4, lon_max+.4 ],\n            dtick = 5\n        ),\n        lataxis = dict (\n            showgrid = True,\n            gridwidth = 0,\n            range= [ lat_min-.4, lat_max+.4 ],\n            dtick = 5\n        )\n    ),\n)\nfig = { \'data\':data, \'layout\':layout }\noff.iplot(fig)\n
Run Code Online (Sandbox Code Playgroud)\n

在此输入图像描述

\n