我想绘制 2 条垂直线来显示两种不同的平均值。使用图面网格再次将其分为不同的子图。
在下面,我在单独的子图中显示
cut
。在每个子图中,我有两种不同的颜色表示 val
。对于每个子图,我想显示 val 和 cut 的平均值。但我目前只显示剪切的平均值。
import seaborn as sns
import plotly.express as px
diamonds = sns.load_dataset('diamonds')
diamonds['val'] = np.random.randint(1, 3, diamonds.shape[0])
grpval = diamonds.groupby(['cut','val'])['price'].mean()
print(grpval)
fig = px.histogram(data_frame=diamonds,
x='price',
facet_col='cut',
color = "val",
facet_col_wrap = 2,
)
for c,idx in zip(diamonds['cut'].unique(),[(1,1),(1,2),(2,1),(2,2),(3,1)]):
df = diamonds[diamonds['cut'] == c]
fig.add_vline(x=df['price'].tail(1).values[0], line_width=1, line_dash='solid', line_color='red', row=idx[0], col=idx[1])
fig.show()
我尝试绘制两种方法,但只得到一种。
以下是向绘图添加两条垂直平均线的方法:
import numpy as np
import seaborn as sns
import plotly.express as px
diamonds = sns.load_dataset("diamonds")
diamonds["val"] = np.random.randint(1, 3, diamonds.shape[0])
grpval = diamonds.groupby(["cut", "val"])["price"].mean()
# reshape the mean values into cut and val dimensions
grpval_per_cut = np.reshape(grpval.values, (diamonds["cut"].unique().size, -1))
# add color sequence to have same colors on histogram and lines
color_discrete_sequence = px.colors.qualitative.G10
fig = px.histogram(
data_frame=diamonds,
x="price",
facet_col="cut",
color="val",
facet_col_wrap=2,
color_discrete_sequence=color_discrete_sequence,
)
# iterate over mean values and subplots (I could not figure out a direct way
# to associate the subplot with the correct mean value, so I arranged it "by hand"
for mean_per_val, (row, col) in zip(
grpval_per_cut, [(3, 1), (3, 2), (2, 2), (2, 1), (1, 1)]
):
for val, line_color in zip(mean_per_val, color_discrete_sequence):
fig.add_vline(
x=val,
line_width=1,
line_dash="solid",
line_color=line_color,
row=row,
col=col,
)
fig.show()