0

I've assigned each coordinate to a cluster using k_means clustering and am hoping to show the density of each cluster with plotly go.Densitymapbox.

import plotly.graph_objects as go

colors=[(0.0,'#170d33' ), (0.058, '#170d33'),
            (0.058, '#2f4b7c'),(0.116,'#2f4b7c'),
            (0.116,'#2f4b7c'),(0.174,'#2f4b7c'),
            (0.174,'#665191'),(0.233,'#665191'),
            (0.233,'#875296'),(0.291,'#875296'),
            (0.291,'#a05195'),(0.348,'#a05195'),
            (0.358,'#d45087'),(0.40,'#d45087'),
            (0.40,'#f95d6a'),(0.465,'#f95d6a'),
            (0.465,'#ff7c43'),(0.523,'#ff7c43'),
            (0.523,'#ffa600'),(0.581,'#ffa600'),
            (0.581,'#ffc332'),(0.638,'#ffc332'),
            (0.638,'#fff629'),(0.69,'#fff629'),
            (0.69,'#f7ffbd'),(0.754,'#f7ffbd'),
            (0.754,'#d2ffbd'),(0.81,'#d2ffbd'),
            (0.81,'#a2ffc9'),(0.86,'#a2ffc9'),
            (0.86,'#85ffd4'),(0.924,'#85ffd4'),
            (0.924,'#1cfff0'),(1.0,'#1cfff0')]

fig=go.Figure(go.Densitymapbox(lat=data['lat'], lon=data['lng'], z=data["cluster"], coloraxis='coloraxis'))
fig.update_layout(height=800, width=1000, coloraxis=dict(colorscale=colors), mapbox_style="open-street-map",
                 mapbox_layers = [
                {
                    "sourcetype": "image",
                    "source": img,
                    "coordinates": bbox_coords
                }], font=dict(size=24), font_family='Open Sans')

fig.show()

Implementing the discrete color scale produces the following: density-clusters not showing

While the default colorscale produces this: enter image description here

It's just difficult to make out the clusters with the default colorscale but I've found almost nothing in the documentation on specifying a discrete colorscale with the density plot, which makes me think it's not really done? I would appreciate any alternative solutions to plotting this that I may be overlooking!

Data can be found below:

{'lat': {0: 43.6,
  1: 45.33,
  2: 32.38,
  3: 34.0,
  4: 30.31,
  5: 37.64,
  6: 34.43,
  7: 31.39,
  8: 33.34,
  9: 40.138306,
  10: 40.68,
  11: 34.28,
  12: 35.862833,
  13: 35.28,
  14: 32.75,
  15: 29.62,
  16: 29.757561,
  17: 35.07,
  18: 45.51,
  19: 27.190719,
  20: 37.51,
  21: 40.41,
  22: 36.81,
  23: 36.81,
  24: 26.2},
 'lng': {0: -96.59,
  1: -122.57,
  2: -94.87,
  3: -84.62,
  4: -97.94,
  5: -122.11,
  6: -119.72,
  7: -97.21,
  8: -86.78,
  9: -88.161651,
  10: -111.82,
  11: -119.29,
  12: -94.196334,
  13: -120.66,
  14: -117.21,
  15: -82.38,
  16: -95.36525,
  17: -82.37,
  18: -122.61,
  19: -80.236942,
  20: -77.69,
  21: -105.01,
  22: -119.87,
  23: -119.87,
  24: -80.15},
 'price': {0: 37000.0,
  1: 99000.0,
  2: 65000.0,
  3: 27800.0,
  4: 52500.0,
  5: 27000.0,
  6: 72000.0,
  7: 25000.0,
  8: 69000.0,
  9: 34500.0,
  10: 55000.0,
  11: 89000.0,
  12: 92000.0,
  13: 72000.0,
  14: 24000.0,
  15: 55000.0,
  16: 37500.0,
  17: 44000.0,
  18: 32500.0,
  19: 39000.0,
  20: 39900.0,
  21: 48500.0,
  22: 59500.0,
  23: 96500.0,
  24: 45500.0},
 'cluster': {0: 14,
  1: 3,
  2: 1,
  3: 4,
  4: 1,
  5: 11,
  6: 2,
  7: 0,
  8: 8,
  9: 9,
  10: 11,
  11: 2,
  12: 2,
  13: 7,
  14: 5,
  15: 1,
  16: 1,
  17: 8,
  18: 15,
  19: 0,
  20: 8,
  21: 10,
  22: 7,
  23: 3,
  24: 0},
 'centroids': {0: 0,
  1: 0,
  2: 0,
  3: 0,
  4: 0,
  5: 0,
  6: 0,
  7: 0,
  8: 0,
  9: 0,
  10: 1,
  11: 0,
  12: 0,
  13: 0,
  14: 0,
  15: 0,
  16: 0,
  17: 0,
  18: 0,
  19: 0,
  20: 0,
  21: 0,
  22: 0,
  23: 0,
  24: 0}}
Erin
  • 465
  • 4
  • 11
  • Your link is `404: Not Found`. *Please* consider sharing a sample of your data as described [here](https://stackoverflow.com/questions/63163251/pandas-how-to-easily-share-a-sample-dataframe-using-df-to-dict/63163254#63163254) – vestland Feb 22 '21 at 22:27
  • 1
    @vestland so sorry about that! I'm updating the question right now. – Erin Feb 22 '21 at 23:12
  • No worries! That happens all the time. And it's why we prefer data samples provided in similar ways as described in the link I provided above. Also, please consider following the steps described in the link, and include your dataset in your code snippet with all necessary imports. As your question now stands, anyone seeking to answer it will have to spend their time recreating your issue instead of finding a solution. So... `df=pd.DataFrame()` – vestland Feb 22 '21 at 23:29
  • @vestland definitely! makes sense. I've included a dictionary of the first 25 rows. Are you familiar with plotly Densitymapbox ? – Erin Feb 23 '21 at 00:10

0 Answers0