离散比例尺地图在美国的应用

2024-07-08 15:08:03 发布

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

当我在python中使用plotly时,我尝试使用离散色阶。我需要一个离散的色阶,因为我为特定城市绘制的某些值与其他所有值相比都太大了,因此离散色阶可以帮助我轻松地将所有值可视化。下面的例子可以更好地解释我的情况: 我有一个数据集,它包含了按城市(在美国)的某些事件的详细信息。 这一事件发生在纽约市50000次,而在美国其他城市,同样的事件发生不到1000次。当我使用连续色阶时,所有其他城市值都会降到低端,而纽约市是唯一一个使用色阶顶部颜色的值。在

提前感谢您的帮助! 谨致问候, 里沙布


Tags: 数据颜色可视化绘制事件情况详细信息plotly
1条回答
网友
1楼 · 发布于 2024-07-08 15:08:03

对于多个商店的10个不同的群集ID,我就是这样生成10个离散的自定义色阶的:

import matplotlib

def matplotlib_to_plotly(cmap, pl_entries):
# Converts matplotlib colormap to plotly colormap. It also shuffles the color map

    h = 1.0/(pl_entries)
    pl_colorscale = []
    c_order = h * np.arange(pl_entries+1)
    c_order_shuffled = c_order.copy()
    # Shuffles the colormap
    np.random.shuffle(c_order_shuffled)
    for i in range(pl_entries):
        C = map(np.uint8, np.array(cmap(c_order_shuffled[i])[:3])*255)
        pl_colorscale.append([c_order[i], 'rgb'+str((C[0], C[1], C[2]))])
        # To have clear boundaries between colors in the colorbar
        if i < (pl_entries):
             pl_colorscale.append([c_order[i+1], 'rgb'+str((C[0], C[1], C[2]))])
    return pl_colorscale

# Sets the colormap of your choice 
cmap = matplotlib.cm.get_cmap('jet')
# Passes the number of distinct colors you need to generate. In this case we have 10 cluster ids in stores_info_df
custom_colorscale = matplotlib_to_plotly(cmap, stores_info_df['CLUSTER_ID'].max())
custom_colorscale

enter image description here

然后,我在plot函数中使用了上面的colorscale:

^{pr2}$

它生成以下绘图。在

enter image description here

相关问题 更多 >

    热门问题