Plotly in Python - 更改标记颜色
Plotly in Python - change marker colors
我想根据值更改标记颜色,所以我尝试了以下代码:
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()
但在这种情况下,所有标记都采用钢蓝色,从第一个选项开始。这里哪里不对?
#!/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()
此代码应该有效。不要更新已经绘制的图形,而是在绘制时更新颜色。
只需将新参数 color_discrete_sequence
直接添加到 fig=px.scatter_geo
并指定您想要的颜色:
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()
我想根据值更改标记颜色,所以我尝试了以下代码:
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()
但在这种情况下,所有标记都采用钢蓝色,从第一个选项开始。这里哪里不对?
#!/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()
此代码应该有效。不要更新已经绘制的图形,而是在绘制时更新颜色。
只需将新参数 color_discrete_sequence
直接添加到 fig=px.scatter_geo
并指定您想要的颜色:
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()