2

I want to change marker colors based on values, so I tried this code:

import plotly.express as px
import pandas as pd

rows=[['501-600','15','122.58333','45.36667'],
      ['till 500','4','12.5','27.5'],
      ['more 1001','41','-115.53333','38.08'],
      ]

colmns=['bins','data','longitude','latitude']
df=pd.DataFrame(data=rows, columns=colmns)
df = df.astype({"data": int})

def SetColor(x):
        if(x == '501-600'):
            return "steelblue"
        elif(x == 'till 500'):
            return "mintcream"
        elif(x == 'more 1001'):
            return "palegoldenrod"

fig=px.scatter_geo(df,lon='longitude', lat='latitude',color='bins',
                      opacity=0.5,size='data',
                      projection="natural earth")

fig.update_traces(marker=dict(symbol='octagon',
                                color=list(map(SetColor, df['bins'])),
                                line=dict(width=0)))

fig.show()

But in this case all markers take steelblue color, from first option. What is not correct here?

Dmitry
  • 361
  • 6
  • 21

2 Answers2

2

Just add new parameter color_discrete_sequence straight into fig=px.scatter_geo and specify colors you want:

import plotly.express as px
import pandas as pd

rows = [['501-600', '15', '122.58333', '45.36667'],
        ['till 500', '4', '12.5', '27.5'],
        ['more 1001', '41', '-115.53333', '38.08'],
        ]

colmns = ['bins', 'data', 'longitude', 'latitude']
df = pd.DataFrame(data=rows, columns=colmns)
df = df.astype({"data": int})

fig = px.scatter_geo(df, lon='longitude', lat='latitude', color='bins',
                     color_discrete_sequence=['green', 'steelblue', 'thistle', 'lime'],
                     opacity=0.5, size='data',
                     projection="natural earth")

fig.update_traces(marker=dict(symbol='octagon',
                              color=list(map(SetColor, df['bins'])),
                              line=dict(width=0)))

fig.show()
Dmitry
  • 361
  • 6
  • 21
1
#!/usr/bin/python3
import plotly.express as px
import pandas as pd

rows=[['501-600','15','122.58333','45.36667'],
      ['till 500','4','12.5','27.5'],
      ['more 1001','41','-115.53333','38.08'],
      ]

colmns=['bins','data','longitude','latitude']
df=pd.DataFrame(data=rows, columns=colmns)
df = df.astype({"data": int})

def SetColor(x):
        if(x == '501-600'):
            return "steelblue"
        elif(x == 'till 500'):
            return "mintcream"
        elif(x == 'more 1001'):
            return "palegoldenrod"

fig=px.scatter_geo(df,lon='longitude', lat='latitude',color=list(map(SetColor, df['bins'])),
                      opacity=0.5,size='data',
                      projection="natural earth")

fig.update_traces(marker=dict(symbol='octagon',
                                line=dict(width=0)))

fig.show()

This code should work. Instead of updating the already plotted graph, update the colors as you are plotting it.

wcshamblin
  • 21
  • 3
  • Thank you for your answer, but code in your answer is changed only label names to the names of colors in function, but not colors themselves ( – Dmitry May 03 '20 at 07:04
  • @Dmitry It works fine for me: https://i.imgur.com/nyS3BAT.png What version of plotly are you running? – wcshamblin May 03 '20 at 13:14
  • just try to make different color, it changes only name of the color in the label, but not color itself. My plotly vers in 4.6.0 – Dmitry May 05 '20 at 11:03
  • @Dmitry I'm running 4.7.1. Consider updating, as the code I wrote works on my version just fine. – wcshamblin May 10 '20 at 15:01
  • I updated to the same version, but still has the same results, can you change the color itself and show in screenshot once again – Dmitry May 10 '20 at 15:21