Plotly:带有下拉菜单和按组颜色的散点图
Plotly: Scatter plot with dropdown menu and color by group
我正在尝试使用 2 个下拉菜单制作一个散点图,其中 select 一个数据列(来自 pandas 数据框)要绘制为 x 轴和 y 轴,但是我我还希望这些点由固定的第三个分类变量着色(这个不需要下拉列表)。
到目前为止,我已经能够使用功能下拉菜单 正确创建散点图,但我不知道如何通过第三个变量为其着色。到目前为止,这是代码:
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
df = px.data.tips().select_dtypes(['number']) # drop non numeric columns
fig = go.Figure(
go.Scatter(
x=df['total_bill'],
y=df['tip'],
hovertemplate='x: %{x} <br>y: %{y}',
mode="markers"))
fig.update_layout(
updatemenus=[
{
"buttons": [
{
"label": f"x - {x}",
"method": "update",
"args": [
{"x": [df[x]]},
{"xaxis": {"title": x}},
],
}
for x in cols
]
},
{
"buttons": [
{
"label": f"y - {x}",
"method": "update",
"args": [
{"y": [df[x]]},
{"yaxis": {"title": x}}
],
}
for x in cols
],
"y": 0.9,
},
],
margin={"l": 0, "r": 0, "t": 25, "b": 0},
height=700)
fig.show()
最终,我想要相同的散点图,但要按“类别”对点进行着色。我可以使用 plotly express 做到这一点,但没有下拉菜单很容易:
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="smoker", trendline="ols")
fig.show()
任何人都知道我如何实现这一点?
如果您了解要使用尺码列进行颜色编码,则可以将该列指定为标记颜色。
fig = go.Figure(go.Scatter(
x=dfs['total_bill'],
y=dfs['tip'],
hovertemplate='x: %{x} <br>y: %{y}',
mode="markers",
marker=dict(color=df['size']) # update
))
如下所述,我能够通过创建一个新列找到解决方案,该列包含与另一列中的分类级别相对应的十六进制颜色代码:
dfx = px.data.tips()
# create list of columns to iterate over for buttons
cols = dfx.columns.values.tolist()
# make list of default plotly colors in hex
plotly_colors=[
'#1f77b4', # muted blue
'#ff7f0e', # safety orange
'#2ca02c', # cooked asparagus green
'#d62728', # brick red
'#9467bd', # muted purple
'#8c564b', # chestnut brown
'#e377c2', # raspberry yogurt pink
'#7f7f7f', # middle gray
'#bcbd22', # curry yellow-green
'#17becf' # blue-teal
]
# create dictionary to associate colors with unique categories
color_dict = dict(zip(dfx['smoker'].unique(),plotly_colors))
# map new column with hex colors to pass to go.Scatter()
dfx['hex']= dfx['smoker'].map(color_dict)
#initialize scatter plot
fig = go.Figure(
go.Scatter(
x=dfx['total_bill'],
y=dfx['tip'],
text=dfx['smoker'],
marker=dict(color=dfx['hex']),
mode="markers"
)
)
# initialize dropdown menus
fig.update_layout(
updatemenus=[
{
"buttons": [
{
"label": f"x - {x}",
"method": "update",
"args": [
{"x": [dfx[x]]},
{"xaxis": {"title": x}},
],
}
for x in cols
]
},
{
"buttons": [
{
"label": f"y - {x}",
"method": "update",
"args": [
{"y": [dfx[x]]},
{"yaxis": {"title": x}}
],
}
for x in cols
],
"y": 0.9,
},
],
margin={"l": 0, "r": 0, "t": 25, "b": 0},
height=700
)
fig.show()
我正在尝试使用 2 个下拉菜单制作一个散点图,其中 select 一个数据列(来自 pandas 数据框)要绘制为 x 轴和 y 轴,但是我我还希望这些点由固定的第三个分类变量着色(这个不需要下拉列表)。
到目前为止,我已经能够使用功能下拉菜单
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
df = px.data.tips().select_dtypes(['number']) # drop non numeric columns
fig = go.Figure(
go.Scatter(
x=df['total_bill'],
y=df['tip'],
hovertemplate='x: %{x} <br>y: %{y}',
mode="markers"))
fig.update_layout(
updatemenus=[
{
"buttons": [
{
"label": f"x - {x}",
"method": "update",
"args": [
{"x": [df[x]]},
{"xaxis": {"title": x}},
],
}
for x in cols
]
},
{
"buttons": [
{
"label": f"y - {x}",
"method": "update",
"args": [
{"y": [df[x]]},
{"yaxis": {"title": x}}
],
}
for x in cols
],
"y": 0.9,
},
],
margin={"l": 0, "r": 0, "t": 25, "b": 0},
height=700)
fig.show()
最终,我想要相同的散点图,但要按“类别”对点进行着色。我可以使用 plotly express 做到这一点,但没有下拉菜单很容易:
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="smoker", trendline="ols")
fig.show()
任何人都知道我如何实现这一点?
如果您了解要使用尺码列进行颜色编码,则可以将该列指定为标记颜色。
fig = go.Figure(go.Scatter(
x=dfs['total_bill'],
y=dfs['tip'],
hovertemplate='x: %{x} <br>y: %{y}',
mode="markers",
marker=dict(color=df['size']) # update
))
如下所述,我能够通过创建一个新列找到解决方案,该列包含与另一列中的分类级别相对应的十六进制颜色代码:
dfx = px.data.tips()
# create list of columns to iterate over for buttons
cols = dfx.columns.values.tolist()
# make list of default plotly colors in hex
plotly_colors=[
'#1f77b4', # muted blue
'#ff7f0e', # safety orange
'#2ca02c', # cooked asparagus green
'#d62728', # brick red
'#9467bd', # muted purple
'#8c564b', # chestnut brown
'#e377c2', # raspberry yogurt pink
'#7f7f7f', # middle gray
'#bcbd22', # curry yellow-green
'#17becf' # blue-teal
]
# create dictionary to associate colors with unique categories
color_dict = dict(zip(dfx['smoker'].unique(),plotly_colors))
# map new column with hex colors to pass to go.Scatter()
dfx['hex']= dfx['smoker'].map(color_dict)
#initialize scatter plot
fig = go.Figure(
go.Scatter(
x=dfx['total_bill'],
y=dfx['tip'],
text=dfx['smoker'],
marker=dict(color=dfx['hex']),
mode="markers"
)
)
# initialize dropdown menus
fig.update_layout(
updatemenus=[
{
"buttons": [
{
"label": f"x - {x}",
"method": "update",
"args": [
{"x": [dfx[x]]},
{"xaxis": {"title": x}},
],
}
for x in cols
]
},
{
"buttons": [
{
"label": f"y - {x}",
"method": "update",
"args": [
{"y": [dfx[x]]},
{"yaxis": {"title": x}}
],
}
for x in cols
],
"y": 0.9,
},
],
margin={"l": 0, "r": 0, "t": 25, "b": 0},
height=700
)
fig.show()