我目前正在使用plotly上的 colored颜色 条来指示放置水下声纳接收器的深度.目前, colored颜色 栏如下所示:

Depths Colorbar

然而,我认为如果较大的数值应该放在底部,而较小的数值应该放在顶部(即设备被放置在离地面更近的地方),这会更有意义,但不知道如何做到这一点.

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

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)

我应该添加什么参数(可能是添加到数据字典中的Colorbar字典中),才能使代表更大深度的数字位于Colorbar的底部?

推荐答案

您可以反转色标,然后通过传递tickvals = [1, 1.1, ... 3.4]来硬编码记号,并使记号文本与之相反:ticktext = ['3.4', '3.3', ... '1'].这还需要您手动将文本" log(meters, 10)"添加到最上面的勾号.

我不知道为什么,但有一个奇怪的行为,在顶部和底部的记号和记号文本被切断-您可能需要调整一些记号的起始值和结束值.这可能与ColorBar的默认填充有关,也可能是我遗漏了什么.

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)

enter image description here

Python相关问答推荐

当密钥是复合且唯一时,Pandas合并抱怨标签不唯一

比较两个二元组列表,NP.isin

将numpy数组存储在原始二进制文件中

如何在BeautifulSoup中链接Find()方法并处理无?

根据不同列的值在收件箱中移动数据

如何在Windows上用Python提取名称中带有逗号的文件?

如果值不存在,列表理解返回列表

聚合具有重复元素的Python字典列表,并添加具有重复元素数量的新键

如何在python polars中停止otherate(),当使用when()表达式时?

在含噪声的3D点网格中识别4连通点模式

启动带有参数的Python NTFS会导致文件路径混乱

判断solve_ivp中的事件

如何在海上配对图中使某些标记周围的黑色边框

Python—为什么我的代码返回一个TypeError

Discord.py -

如何在Python请求中组合多个适配器?

如何将一组组合框重置回无 Select tkinter?

如何在Python中自动创建数字文件夹和正在进行的文件夹?

有没有一种方法可以在朗肯代理中集成向量嵌入

如何在PYTHON中向单元测试S Side_Effect发送额外参数?