将对象颜色绘制成相似图
plotly graph object colors to similar
我尝试使用 plotly graph 对象绘制值,并使用一列设置颜色。
fig_1.add_trace(
go.Scattermapbox(
lat=data.latitude,
lon=data.longitude,
marker=go.scattermapbox.Marker(size=9, color=data.id_nr),
)
)
然而,这些值是非常大的数字(id 数字,int64),因此,彼此更接近的 id 数字(100k 不同而不是 1M)将显示几乎相同的颜色。
有没有办法将颜色设置为离散颜色? 使用
... color=data.id_nr.astype(str)
在 Plotly Express 中用于使冷却器分离的方法不起作用。
Invalid element(s) received for the 'color' property of
scattermapbox.marker
基本问题是:您能否设置每个值的颜色,无论增量有多近或多远,都获得独特的颜色?
编辑:
id 值更像是:
id=[1,2,5,100004,100007,100009]
结合plotly连续上色,前三个和后三个颜色差不多。
Plotly Express 通过将(id 的)int 值更改为字符串来解决此问题,使它们离散。
编辑2:
一种解决方案是按 ID 分隔数据,然后为每个 ID 添加跟踪。然而,这不是......“性感”,我宁愿知道一个解决方案,可以离散地处理颜色。
我重新创建了数据并通过改编 official reference to your assignment. The format of the data is a data frame with three columns: id column, latitude and longitude, and location name. In the definition of the marker, I used the id column to define the color, and specified 'Viridis' as the color scale, which is a continuous color map. See this for a concrete example 在散点图中引入色标的示例创建了代码。
import pandas as pd
import plotly.express as px
lat = [38.91427,38.91538,38.91458,38.92239,38.93222,38.90842,38.91931,38.93260,38.91368,38.88516,38.921894,38.93206, 38.91275]
lon = [-77.02827,-77.02013,-77.03155,-77.04227,-77.02854,-77.02419,-77.02518,-77.03304,-77.04509,-76.99656,-77.042438,-77.02821,-77.01239]
name = ["The coffee bar","Bistro Bohem","Black Cat", "Snap","Columbia Heights Coffee","Azi's Cafe", "Blind Dog Cafe","Le Caprice","Filter","Peregrine","Tryst","The Coupe","Big Bear Cafe"]
ids = [1,2,3,4,5001,5002,5003,5004,100004,100007,100009,100010,100011]
colors = px.colors.qualitative.Alphabet[:len(lat)]
df = pd.DataFrame({'id':ids, 'lat':lat,'lon':lon,'name':name,'colors': colors})
df.head()
id lat lon name colors
0 1 38.91427 -77.02827 The coffee bar #AA0DFE
1 2 38.91538 -77.02013 Bistro Bohem #3283FE
2 3 38.91458 -77.03155 Black Cat #85660D
3 4 38.92239 -77.04227 Snap #782AB6
4 5001 38.93222 -77.02854 Columbia Heights Coffee #565656
import plotly.graph_objects as go
mapbox_access_token = open("mapbox_api_key.txt").read()
fig = go.Figure(go.Scattermapbox(
lat=df['lat'],
lon=df['lon'],
mode='markers',
marker=go.scattermapbox.Marker(
size=16,
color=df['colors'],
#colorscale='Viridis'
),
text=df['name'],
))
fig.update_layout(
autosize=False,
width=1000,
height=500,
hovermode='closest',
mapbox=dict(
accesstoken=mapbox_access_token,
bearing=0,
center=dict(
lat=38.92,
lon=-77.07
),
pitch=0,
zoom=11
),
)
fig.show()
我尝试使用 plotly graph 对象绘制值,并使用一列设置颜色。
fig_1.add_trace(
go.Scattermapbox(
lat=data.latitude,
lon=data.longitude,
marker=go.scattermapbox.Marker(size=9, color=data.id_nr),
)
)
然而,这些值是非常大的数字(id 数字,int64),因此,彼此更接近的 id 数字(100k 不同而不是 1M)将显示几乎相同的颜色。
有没有办法将颜色设置为离散颜色? 使用
... color=data.id_nr.astype(str)
在 Plotly Express 中用于使冷却器分离的方法不起作用。
Invalid element(s) received for the 'color' property of scattermapbox.marker
基本问题是:您能否设置每个值的颜色,无论增量有多近或多远,都获得独特的颜色?
编辑: id 值更像是:
id=[1,2,5,100004,100007,100009]
结合plotly连续上色,前三个和后三个颜色差不多。 Plotly Express 通过将(id 的)int 值更改为字符串来解决此问题,使它们离散。
编辑2:
一种解决方案是按 ID 分隔数据,然后为每个 ID 添加跟踪。然而,这不是......“性感”,我宁愿知道一个解决方案,可以离散地处理颜色。
我重新创建了数据并通过改编 official reference to your assignment. The format of the data is a data frame with three columns: id column, latitude and longitude, and location name. In the definition of the marker, I used the id column to define the color, and specified 'Viridis' as the color scale, which is a continuous color map. See this for a concrete example 在散点图中引入色标的示例创建了代码。
import pandas as pd
import plotly.express as px
lat = [38.91427,38.91538,38.91458,38.92239,38.93222,38.90842,38.91931,38.93260,38.91368,38.88516,38.921894,38.93206, 38.91275]
lon = [-77.02827,-77.02013,-77.03155,-77.04227,-77.02854,-77.02419,-77.02518,-77.03304,-77.04509,-76.99656,-77.042438,-77.02821,-77.01239]
name = ["The coffee bar","Bistro Bohem","Black Cat", "Snap","Columbia Heights Coffee","Azi's Cafe", "Blind Dog Cafe","Le Caprice","Filter","Peregrine","Tryst","The Coupe","Big Bear Cafe"]
ids = [1,2,3,4,5001,5002,5003,5004,100004,100007,100009,100010,100011]
colors = px.colors.qualitative.Alphabet[:len(lat)]
df = pd.DataFrame({'id':ids, 'lat':lat,'lon':lon,'name':name,'colors': colors})
df.head()
id lat lon name colors
0 1 38.91427 -77.02827 The coffee bar #AA0DFE
1 2 38.91538 -77.02013 Bistro Bohem #3283FE
2 3 38.91458 -77.03155 Black Cat #85660D
3 4 38.92239 -77.04227 Snap #782AB6
4 5001 38.93222 -77.02854 Columbia Heights Coffee #565656
import plotly.graph_objects as go
mapbox_access_token = open("mapbox_api_key.txt").read()
fig = go.Figure(go.Scattermapbox(
lat=df['lat'],
lon=df['lon'],
mode='markers',
marker=go.scattermapbox.Marker(
size=16,
color=df['colors'],
#colorscale='Viridis'
),
text=df['name'],
))
fig.update_layout(
autosize=False,
width=1000,
height=500,
hovermode='closest',
mapbox=dict(
accesstoken=mapbox_access_token,
bearing=0,
center=dict(
lat=38.92,
lon=-77.07
),
pitch=0,
zoom=11
),
)
fig.show()