How to fill a scatter plot map with specific values?

I have created a very basic map, but I want to fill specific districts with colors representing certain values.

Is there anyway to do this in Plotly?

import plotly

trace1 = [dict(
  type = 'scatter',
  x = provinces['x'],
  y = provinces['y'],
    showlegend=False,
  mode = 'lines',
   line = dict(
        color = ('rgb(0, 0, 0)'),
        width = 0.8),
  transforms = [dict(
    type = 'groupby',
    groups = provinces['shapeid'])])]

trace2 = [dict(
  type = 'scatter',
  x = districts['x'],
  y = districts['y'],
  hoveron = 'fills',
  showlegend=False,
  mode = 'lines',
   line = dict(
        color = ('rgb(0, 0, 0)'),
        width = 0.1),
  transforms = [dict(
    type = 'groupby',
    groups = districts['shapeid'])])]

all_traces = trace1 + trace2
plotly.offline.plot(all_traces, validate=False)

Hi @localh85,

If each of your districts is a separate scatter trace then you could set the scatter.fill property to 'toself', which will cause the region enclosed by the scatter line to be filled. This is how the county choropleth figure factory works (https://plot.ly/python/county-choropleth/#change-the-scope).

Hope that helps!
-Jon

Thanks this is helpful! I think to get the fill β€˜toself’ to work, I need to restructure my data.

Do you know of any example data that would look a lot like this: https://plot.ly/python/county-choropleth/#change-the-scope, without relying on FIPS codes? This is because I need to figure out how to structure my lat/lon such that they are all contained in a single row somehow, which is what I think is necessary to get the fill to work correctly for each geographic entity. My data is like this currently:

|shapeid|x|y|PROV_NA_EN|
|---|---|---|---|
|0|65.47408796|33.95026458|Ghor|
|0|65.4851627|33.93754394|Ghor|
|0|65.48328404|33.92169049|Ghor|
|0|65.44034004|33.9070541|Ghor|
|0|65.41493185|33.86796457|Ghor|
|0|65.42222856|33.85154697|Ghor|
|0|65.4101108|33.84164429|Ghor|
|0|65.44057982|33.82054924|Ghor|
|0|65.44728595|33.80161132|Ghor|
|0|65.47801512|33.7994485|Ghor|
|0|65.47565092|33.79382476|Ghor|
|0|65.50353478|33.76229254|Ghor|
|0|65.50874672|33.73975091|Ghor|
|0|65.55369968|33.73649345|Ghor|
|0|65.58379862|33.75186867|Ghor|
|0|65.60581906|33.73102092|Ghor|
|0|65.61142189|33.71264884|Ghor|
|0|65.65142351|33.70821869|Ghor|

Ok so I have engineered a way to get the fill colors (associated code below).

I am wondering if I want to do an entire country, do I really have to replicate this process 398 more times to create every a unique trace for every district and province so I can have custom fill colors?

provinces = pd.read_csv('https://raw.githubusercontent.com/rocketfish88/democ/master/t1.csv')
districts = pd.read_csv('https://raw.githubusercontent.com/rocketfish88/democ/master/t2.csv')

helmand = provinces.loc[provinces['PROV_NA_EN'] == 'Hilmand']
nimruz = provinces.loc[provinces['PROV_NA_EN'] == 'Nimroz']

x1 = helmand['x'].values
y1 = helmand['y'].values

x2 = nimruz['x'].values
y2 = nimruz['y'].values

x1 = tuple(x1.reshape(1, -1)[0])
y1 = tuple(y1.reshape(1, -1)[0])

x2 = tuple(x2.reshape(1, -1)[0])
y2 = tuple(y2.reshape(1, -1)[0])

trace1 = [dict(
  type = 'scatter',
  x = x1,
  y = y1,
    showlegend=False,
    fill = 'toself',
    fillcolor = 'rgba(255, 182, 193, .4)',
  mode = 'lines',
   line = dict(
        color = ('rgb(0, 0, 0)'),
        width = 0.8))]

trace2 = [dict(
  type = 'scatter',
  x = x2,
  y = y2,
  fill = 'toself',
  fillcolor = 'rgba(152, 0, 0, .8)',
  showlegend=False,
  mode = 'lines',
   line = dict(
        color = ('rgb(0, 0, 0)'),
        width = 0.1))]

all_traces = trace1 + trace2
plotly.offline.plot(all_traces, validate=False)

Hi @localh85,

I am wondering if I want to do an entire country, do I really have to replicate this process 398 more times to create every a unique trace for every district and province so I can have custom fill colors?

Yeah, that’s really the only way to get unique colors across all of the shapes. So it will admittedly be a bit slow to for pan/zoom interactions. This is why the country choropleth figure factory disables zoom interactions.

-Jon

I appreciate the help!