繁体   English   中英

Plotly 中的连续色标 -- plotly.express 为 px,plotly.graph_objects 为 go

[英]Continuous Color Scales in Plotly -- plotly.express as px, and plotly.graph_objects as go

我正在尝试使用以下代码使用color_continuous_scale='Reds'控制px.scatter中的color scale

sca = px.scatter(df_centroid_Coord, x="x", y="y", 
                 title="Southern Region Centroids", 
                 color='Ambulance_Treatment_Time', 
                 #hover_name="KnNamn",
                 #hover_data= ['Ambulance_Treatment_Time', "TotPop"],
                 log_x=True,
                 size_max=60, 
                 color_continuous_scale='Reds',
                 range_color=(0.5,2),
                 )

sca.update_traces(marker={'size': 4, 'symbol': 1}) 

Output: 在此处输入图像描述

然后,我使用以下代码在同一图表上绘制了更多数据:

fig = go.Figure()
fig.add_trace(go.Scatter(sca.data[0]))
fig.add_trace(go.Scatter(x=df_station['x'],
                         y=df_station['y'],
                         #color= 'Ambulance_Treatment_Time',
                         #marker_colorscale=px.colors.sequential.Reds,
                         #marker_colorscale=px.colors.sequential.Viridis,
                         mode='markers+text',
                         text=df_station['Ambulance station name'],
                         textposition='top center',
                         showlegend=True,
                         marker=dict(
                             size=8,
                             symbol=2,
                             color='black'
                         )
                        )
             )

#go.Scatter(marker_colorscale=plotly.colors.sequential.Viridis)

#fig.add_trace(color_continuous_scale=px.colors.sequential.Viridis)
#fig.add_trace((go.Scatter(marker_colorscale=px.colors.sequential.Oranges)))
#fig.update_traces()

fig.update_layout(width=1000, height=900, paper_bgcolor="LightSteelBlue")

fig.show()

Output:

在此处输入图像描述

在上面的结果图中, the color_continuous_scale默认设置为"Viridis" 但是,我想将其更改color scale as red 我尝试使用marker_colorscale=px.colors.sequential.Reds, or color_continuous_scale='Reds'但这些属性不起作用,因为在第二个数据框中没有数字序列来进行颜色编码。

这是完整的代码:

import plotly.express as px
import plotly.graph_objects as go
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
from openpyxl import load_workbook
import numpy as np
import pandas as pd
from mplcursors import cursor

import numpy as np; 
np.random.seed(0)
import seaborn as sns;




data = '''
x   y   Base_TT
14.1315559  55.75811117 1.871884861
14.66228957 57.02751992 1.599971112
14.49407157 56.06563489 1.307165278
13.21996788 55.4786748  1.411554445
14.00501286 55.72120854 1.968138334
12.73736102 56.71097302 1.309849028
14.56668525 56.74872925 1.719116945
13.24138764 56.41359089 2.000620417
14.94308088 56.54283706 1.668724723
14.5744739  56.05695327 1.266861528
'''

df_centroid_Coord = pd.read_csv(io.StringIO(data), delim_whitespace=True)


data = '''
ID "Ambulance station name" Longtitude Latitude
0 1 AImhult 14.128734 56.547992
1 2 Angelhdm 12.870739 56.242114
2 3 Alvesta 14.549503 56.920740
3 4 "Ostra Ljungby" 13.057450 56.188099
4 5 Broby 14.080958 56.254481
5 6 Bromölla 14.466869 56.072272
6 7 Försláv 12.814913 56.350098
7 9 Hasslehdm 13.778234 56.161536
8 10 Haganas 12.556995 56.206016
9 11 Hörby 13.643265 55.849811
10 12 "Halmstad.Vaster" 12.819960 56.674306 
'''

df_station = pd.read_csv(io.StringIO(data), delim_whitespace=True) 
df_station.rename(columns={'Longtitude':'x', 'Latitude':'y'}, inplace=True)
df_centroid_Coord['Ambulance_Treatment_Time'] = df_centroid_Coord ['Base_TT']

sca = px.scatter(df_centroid_Coord, x="x", y="y", 
                 title="Southern Region Centroids", 
                 color='Ambulance_Treatment_Time', 
                 #hover_name="KnNamn",
                 #hover_data= ['Ambulance_Treatment_Time', "TotPop"],
                 log_x=True,
                 size_max=60, 
                 color_continuous_scale='Reds',
                 range_color=(0.5,2),
                 )

sca.update_traces(marker={'size': 4, 'symbol': 1})

fig = go.Figure()
fig.add_trace(go.Scatter(sca.data[0]))
fig.add_trace(go.Scatter(x=df_station['x'],
                         y=df_station['y'],
                         #color= 'Ambulance_Treatment_Time',
                         #marker_colorscale=px.colors.sequential.Reds,
                         #marker_colorscale=px.colors.sequential.Viridis,
                         mode='markers+text',
                         text=df_station['Ambulance station name'],
                         textposition='top center',
                         showlegend=True,
                         marker=dict(
                             size=8,
                             symbol=2,
                             color='black'
                         )
                        )
             )

#go.Scatter(marker_colorscale=plotly.colors.sequential.Viridis)

#fig.add_trace(color_continuous_scale=px.colors.sequential.Viridis)
#fig.add_trace((go.Scatter(marker_colorscale=px.colors.sequential.Oranges)))
#fig.update_traces()

fig.update_layout(width=1000, height=900, paper_bgcolor="LightSteelBlue")

fig.show()

你应该在第一个上添加新的情节,我有少量样本到 plot,你可以根据需要玩其他配置:

import plotly.express as px
import plotly.graph_objects as go
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
from openpyxl import load_workbook
import numpy as np
import pandas as pd
from io import StringIO

import numpy as np; 
np.random.seed(0)
import seaborn as sns;




data = '''
x   y   Base_TT
14.1315559  55.75811117 1.871884861
14.66228957 57.02751992 1.599971112
14.49407157 56.06563489 1.307165278
13.21996788 55.4786748  1.411554445
14.00501286 55.72120854 1.968138334
12.73736102 56.71097302 1.309849028
14.56668525 56.74872925 1.719116945
13.24138764 56.41359089 2.000620417
14.94308088 56.54283706 1.668724723
14.5744739  56.05695327 1.266861528
'''

df_centroid_Coord = pd.read_csv(StringIO(data), delim_whitespace=True)


data = '''
ID "Ambulance station name" Longtitude Latitude
0 1 AImhult 14.128734 56.547992
1 2 Angelhdm 12.870739 56.242114
2 3 Alvesta 14.549503 56.920740
3 4 "Ostra Ljungby" 13.057450 56.188099
4 5 Broby 14.080958 56.254481
5 6 Bromölla 14.466869 56.072272
6 7 Försláv 12.814913 56.350098
7 9 Hasslehdm 13.778234 56.161536
8 10 Haganas 12.556995 56.206016
9 11 Hörby 13.643265 55.849811
10 12 "Halmstad.Vaster" 12.819960 56.674306 
'''

df_station = pd.read_csv(StringIO(data), delim_whitespace=True) 
df_station.rename(columns={'Longtitude':'x', 'Latitude':'y'}, inplace=True)
df_centroid_Coord['Ambulance_Treatment_Time'] = df_centroid_Coord ['Base_TT']

fig = px.scatter(df_centroid_Coord, x="x", y="y", 
                 title="Southern Region Centroids", 
                 color='Ambulance_Treatment_Time', 
                 log_x=True,
                 size_max=60, 
                 color_continuous_scale='Reds',
                 range_color=(0.5,2),
                 )

fig.update_traces(marker={'size': 4, 'symbol': 1})
fig.add_trace(go.Scatter(x=df_station['x'],
                         y=df_station['y'],
                         mode='markers+text',
                         text=df_station['Ambulance station name'],
                         textposition='top center',
                         showlegend=True,
                         marker=dict(
                             size=8,
                             symbol=2,
                             color='black'
                         )
                        )
             )



fig.show()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM