如何在 plotly scattergeo 中自定义颜色条?

How can I customize the colorbar in plotly scattergeo?

我从 NASA 地球数据网站(南美洲的火灾)中提取了一些火灾数据,并将这些数据绘制在世界地图上。我用一个颜色条来显示每场火灾的亮度。

火焰亮度的变化不对应于完整的色标范围,并且大多数火焰的颜色相同(黄色)。这是我的代码:

import csv

from plotly.graph_objs import Scattergeo, Layout
from plotly import offline

filename = 'data/MODIS_C6_South_America_24h.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    print(header_row)

    # Get latitudes, longitudes and brightness from this file.

    lats, lons, brights = [], [], []
    for row in reader:
        lat = float(row[0])
        lats.append(lat)
        lon = float(row[1])
        lons.append(lon)
        bright = float(row[2])
        brights.append(bright)

# Map the fires
data = [{
    'type': 'scattergeo',
    'lon': lons,
    'lat': lats,
    'marker': {
        'size': [1/30* bright for bright in brights],
        'color': brights,
        'colorscale': 'Inferno',
        'reversescale': True,
        'colorbar': {'title': 'Brightness'},
    },
}]
my_layout = Layout(title='South America Fires\npast 24 hours')

fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='south_america_fires.html')

我能否以某种方式更改色标的限制,使标记具有更宽的颜色范围并且更好区分?还是有更好的策略?

The variance in brightness of the fires does not correspond to the full colorscale range

是的,他们有。只需查看更简单的数据可视化:

图 1: Seaborn 分布图

代码 1:Seaborn 分布图

import seaborn as sns
import numpy as np
sns.set(color_codes=True)
sns.distplot(tuple(brights))

你的情节最终看起来像这样,原因有以下三个:

  1. brightness = 330
  2. 周围有许多个观测值
  3. 很少 观察到更亮的火
  4. 最重要的是,标记会按照它们在数据集中出现的顺序添加到图中。

因此,如果您只是对数据进行排序以确保较亮的火不被较暗的火覆盖,您将得到:

*绘图 2: 使用 brights.sort()

排序 brights

我认为应该解决这个问题:

[...] so that the markers have a broader color range and are better distinguishable?

所以真的没有必要担心这个:

Can I somehow change the limits of the colorscale [...]

也可以考虑对您的数据进行日志重新编码。我测试了它,但它并没有造成太大的视觉差异。请注意,我删除了 'size': [1/60* bright for bright in brights] 部分。我认为情节 2 看起来比这更好:

完整代码:

import csv

from plotly.graph_objs import Scattergeo, Layout
from plotly import offline

filename = 'C:\pySO\MODIS_C6_South_America_24h.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    print(header_row)

# Get latitudes, longitudes and brightness from this file.

    lats, lons, brights = [], [], []
    for row in reader:
        lat = float(row[0])
        lats.append(lat)
        lon = float(row[1])
        lons.append(lon)
        bright = float(row[2])
        brights.append(bright)

brights.sort()

# Map the fires
data = [{
    'type': 'scattergeo',
    'lon': lons,
    'lat': lats,
    'marker': {
        #'size': [1/60* bright for bright in brights],
        'color': brights,
        #'color': brights.sort(),
        'colorscale': 'Inferno',
        'reversescale': True,
        'colorbar': {'title': 'Brightness'},
    },
}]
my_layout = Layout(title='South America Fires\npast 24 hours')

fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='south_america_fires.html')