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

标签 python plotly plotly-python

enter image description here我从 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 分布图

enter image description here

代码 1:Seaborn 分布图

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

你的 plotly 最终看起来像现在这样有三个原因:

  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 [...]

也可以考虑对数据进行日志重新编码。我测试了它,但它并没有产生太大的视觉差异。请注意,我删除了 'size': [1/60* Bright for Brights 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')

关于python - 如何在plotly scattergeo中自定义颜色条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60577525/

相关文章:

python - 有效地删除列表的第一个元素

python - Plotly:如何显示边际直方图计数以外的其他值?

python - scapy 摘要 DNS Ans

python - 使用 Python 通过 POST 多部分表单数据上传文件

python - Confluence WIki 页面未通过 API 更新

python - 绘制 yaxis2 手动缩放

r - 在堆积区域图中将悬停文本格式化为 $ (R/Plot.ly)

python - 通过 pandas 数据框迭代绘制日期范围之间的水平线

python - 动态更新颜色栏标题 - Plotly

python - 密谋:如何从x轴上删除空日期?