与多个类别的块框图
考虑以下玩具数据:
import pandas as pd
import numpy as np
from plotly import graph_objects as go
from plotly.subplots import make_subplots
np.random.seed(42)
df = pd.DataFrame(
{
"val1": np.random.normal(0, 1, size=100),
"val2": np.random.normal(5, 2, size=100),
"cat": np.random.choice(["a", "b"], size=100),
}
)
哪些产量(前5行):
Val1 | Val2 | CAT | |
---|---|---|---|
0 | 0.496714 | 2.16926 | B |
1 | -0.138264 | 4.15871 | B |
2 | 0.647689 | 4.31457 | A |
3 | 1.52303 | 3.39545 | B |
4 | -0.234153 | 4.67743 | 。 |
我的目标是让每个目标都包含两个盒子,一个盒子(一个盒子)(一个盒子)(一个盒子)(一个盒子)(一个盒子)(一个盒子)(一个盒子)
以下代码:
fig = make_subplots(rows=2, cols=1, subplot_titles=["Value 1 dist", "Value 2 dist"])
fill_colors = {"a": "rgba(150, 25, 40, 0.5)", "b": "rgba(25, 150, 40, 0.5)"}
for i, val in enumerate(["val1", "val2"]):
for c in df["cat"].unique():
dff = df[df["cat"] == c]
fig.add_trace(
go.Box(
y=dff[val],
x=dff["cat"],
boxmean="sd",
name=c,
showlegend=True if val=="val1" else False,
fillcolor=fill_colors[c],
line={"color": fill_colors[c]},
),
row=i + 1,
col=1,
)
使我非常接近:
- 情节周期?因此,结果与其他图兼容。请注意,我对颜色进行了硬编码...
- 左侧的传奇;是否有一种只有单个传说的程序化方法?请注意,如果val ==“ val1” else false ,我使用
showlegend = true。
- 奖励:我如何控制盒子的顺序(即
我在过去两个相关问题中发布的盒子的顺序( 例如,哪个类别是第一个?) /a> and 在这里),但是那里的答案并没有帮助我按照我的意愿调整我的情节。
Consider the following toy data:
import pandas as pd
import numpy as np
from plotly import graph_objects as go
from plotly.subplots import make_subplots
np.random.seed(42)
df = pd.DataFrame(
{
"val1": np.random.normal(0, 1, size=100),
"val2": np.random.normal(5, 2, size=100),
"cat": np.random.choice(["a", "b"], size=100),
}
)
which yields (top 5 rows):
val1 | val2 | cat | |
---|---|---|---|
0 | 0.496714 | 2.16926 | b |
1 | -0.138264 | 4.15871 | b |
2 | 0.647689 | 4.31457 | a |
3 | 1.52303 | 3.39545 | b |
4 | -0.234153 | 4.67743 | a |
My objective is to get two box plots each containing two boxes (one per category).
Following code:
fig = make_subplots(rows=2, cols=1, subplot_titles=["Value 1 dist", "Value 2 dist"])
fill_colors = {"a": "rgba(150, 25, 40, 0.5)", "b": "rgba(25, 150, 40, 0.5)"}
for i, val in enumerate(["val1", "val2"]):
for c in df["cat"].unique():
dff = df[df["cat"] == c]
fig.add_trace(
go.Box(
y=dff[val],
x=dff["cat"],
boxmean="sd",
name=c,
showlegend=True if val=="val1" else False,
fillcolor=fill_colors[c],
line={"color": fill_colors[c]},
),
row=i + 1,
col=1,
)
Brings me very close:
Here are the things I would like to adjust:
- How do I get, programmatically, the first 2 (or
n
) colors used in the default cycle of Plotly? So the result is compatible with other plots. Note that I hardcoded the colors... - The legend on the left; is there a more programmatic way to have only single legend? Note that I used
showlegend=True if val=="val1" else False
. - Bonus: How can I control the order of the boxes (i.e. which category comes first?)
I posted in the past two related questions (here and here) but the answers there didn't help me tune me plot as I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅官方参考如何获得颜色
标准颜色集的名称。您可以在一个中获得颜色名称
列表。
关于控制重复的传奇,我个人没有
我使用方法的问题,这是一种常见的方法,
但是,如果我要编程处理,我会使用
set()
来通过添加重复的传奇名称来使其与众不同。我学到了这个
提示来自 a>。
第三个是按类别订购,您可以指定上升或
按类别下降订单。
这是没有得到预期答案的人的回应。我以前的答案不令人满意?我会尽可能回复。
Please refer to the official reference for how to get the color
names for a standard color set. You can get the color names in a
list.
As for controlling duplicate legends, I personally don't have a
problem with your method as I use it and it is a common approach,
but if I were to handle it programmatically, I would use
set()
tomake it unique by adding the duplicate legend names. I learned this
Tips from this answer.
The third is to order by category, you can specify ascending or
descending order by category.
This is a response from someone who did not get the expected answer. What was unsatisfactory about my previous answers? I will respond whenever possible.