Для 10 различных идентификаторов кластера для нескольких магазинов, вот как я сгенерировал 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
Затем я использовалвышеуказанная цветовая шкала в функции графика:
def visualize_geo_store_clusters(stores_info_df, fig_name='store_similarity_US_map', cluster_id = 'CLUSTER_ID'):
max_cluster_id = stores_info_df[cluster_id].max()
data = [ dict(
type = 'scattergeo',
locationmode = 'USA-states',
lon = stores_info_df['LONGTITUDE'],
lat = stores_info_df['LATITUDE'],
text = stores_info_df['TEXT'],
mode = 'markers',
marker = dict(
colorscale= custom_colorscale,
cmin = stores_info_df[cluster_id].min(),
color = stores_info_df[cluster_id],
cmax = max_cluster_id,
colorbar = dict(
title = 'Cluster ID',
titleside = 'top',
tickmode = 'array',
tickvals = np.arange(1, max_cluster_id+1),
ticktext = np.arange(1, max_cluster_id+1),
#ticks = 'outside',
)
))]
layout = dict(
title = 'Similarity between Stores',
geo = dict(
scope='usa',
projection=dict( type='albers usa' ),
showland = True,
landcolor = "rgb(250, 250, 250)",
subunitcolor = "rgb(217, 217, 217)",
countrycolor = "rgb(217, 217, 217)",
countrywidth = 0.5,
subunitwidth = 0.5
),
)
fig = dict(data=data, layout=layout)
plotly.offline.iplot(fig, validate=False)
Создает следующий график.