简体   繁体   English

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

[英]How can I customize the colorbar in plotly scattergeo?

在此处输入图片说明 I pulled some fire data from the NASA earthdata website (fires in south america) and plotted the data on a world map.我从 NASA earthdata 网站(南美洲的火灾)中提取了一些火灾数据,并将这些数据绘制在世界地图上。 I used a colorbar to display the brightness of each fire.我使用了一个颜色条来显示每个火的亮度。

The variance in brightness of the fires does not correspond to the full colorscale range and most of the fires are in the same color (yellow).火焰亮度的变化并不对应于完整的色阶范围,并且大多数火焰具有相同的颜色(黄色)。 Here is my code:这是我的代码:

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

Can I somehow change the limits of the colorscale so that the markers have a broader color range and are better distinguishable?我能否以某种方式更改色阶的限制,以便标记具有更宽的颜色范围并且更易于区分? Or is there better strategy?或者有更好的策略吗?

The variance in brightness of the fires does not correspond to the full colorscale range火焰亮度的变化与全色阶范围不对应

Yes, they do.是的,他们这样做。 Just have look at a simpler visualization of your data:只需查看更简单的数据可视化:

Plot 1: Seaborn distribution plot图 1: Seaborn 分布图

在此处输入图片说明

Code 1: Seaborn distribution plot代码 1: Seaborn 分布图

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

Your plot just ends up looking as it does for three reasons:由于三个原因,您的情节最终看起来像它一样:

  1. There are many observations around brightness = 330 brightness = 330附近有很多观察
  2. There are very few observations of the brighter fires对更明亮的火灾的观察很少
  3. And most importanntly, the markers are added to the plot in the order they appear in your dataset.最重要的是,标记会按照它们在数据集中出现的顺序添加到图中

So if you just sort the data to make sure that the brighter fires aren't covered by the less brighter fires, you'll get this:因此,如果您只是对数据进行排序以确保较亮的火没有被较不亮的火覆盖,您将得到:

*Plot 2: Sorted brights using brights.sort() *剧情2:分类brights使用brights.sort()

在此处输入图片说明

I think that should take care of this:我认为应该解决这个问题:

[...] so that the markers have a broader color range and are better distinguishable? [...] 以便标记具有更广泛的颜色范围并且更易于区分?

So there's really no need to worry about this:所以真的没有必要担心这个:

Can I somehow change the limits of the colorscale [...]我可以以某种方式改变色阶的限制 [...]

You could consider a log recoding of your data as well.也可以考虑对数据进行日志记录。 I tested it, but it didn't make much of a visual difference.我测试了它,但它并没有产生太大的视觉差异。 And do note that I removed the 'size': [1/60* bright for bright in brights] part.请注意,我删除了'size': [1/60* bright for bright in brights]部分。 I think plot 2 looked better than this:我认为情节 2 看起来比这更好:

在此处输入图片说明

Complete code:完整代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM