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

2024-07-04 13:07:40 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here我从美国宇航局地球数据网站(南美洲的火灾)上获取了一些火灾数据,并将这些数据绘制在世界地图上。我用一个色条来显示每个火的亮度

火焰亮度的变化与全色标范围不一致,大多数火焰颜色相同(黄色)。这是我的密码:

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')

我是否可以改变色标的限制,使标记具有更宽的颜色范围并更好地区分?还是有更好的策略


Tags: csv数据fromimportdatafloatfilenamereader
1条回答
网友
1楼 · 发布于 2024-07-04 13:07:40

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

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

图1:海本分布图

enter image description here

代码1:Seaborn分布图

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

由于以下三个原因,您的情节最终看起来像这样:

  1. brightness = 330周围有许多观测
  2. 对明亮火焰的观测很少
  3. 最重要的是,标记按它们在数据集中出现的顺序添加到绘图中

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

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

enter image description here

我认为应该注意这一点:

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

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

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

<>你也可以eEM>考虑你的数据的日志记录。我对它进行了测试,但在视觉上没有太大的差别。请注意,我删除了'size': [1/60* bright for bright in brights]部分。我认为情节2看起来比这更好:

enter image description here

完整代码:

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')

相关问题 更多 >

    热门问题