简体   繁体   English

我可以将 vline 和 hline 添加到 plotly plot 忽略边缘图吗?

[英]Can I add a vline and hline to a plotly plot ignoring marginal plots?

I am moving a visualization from the seaborn library to the plotly library.我正在将可视化从 seaborn 库移动到 plotly 库。 I want a scatterplot with marginal histograms for my x and y variables.我想要一个带有 x 和 y 变量边缘直方图的散点图。 I want to show a vertical and horizontal line for the average of my x and y variables.我想为我的 x 和 y 变量的平均值显示一条垂直线和水平线。

To show my problem I created a dummy dataframe of random X and Y values为了显示我的问题,我创建了一个随机 X 和 Y 值的虚拟 dataframe

import pandas as pd
import numpy as np
import seaborn as sns
import plotly.express as px

df = pd.DataFrame(np.random.randint(0,100,size=(100, 2)), columns=list('XY'))

I want to replicate the seaborn library output on plotly.我想在 plotly 上复制 seaborn 库 output。

g = sns.JointGrid(data=df, x="X", y="Y")
g.plot(sns.scatterplot, sns.histplot)
g.refline(x=df.X.mean(), y=df.Y.mean())

plt.show()

海底情节

When I do something similar on plotly using add_line and add_hline I get the following:当我使用add_lineadd_hline在 plotly 上做类似的事情时,我得到以下信息:

df = pd.DataFrame(np.random.randint(0,100,size=(100, 2)), columns=list('XY'))
fig = px.scatter(df, x='X', y='Y',
                marginal_y="histogram", marginal_x="histogram",
                width=800, height=600)
fig.add_hline(y=df['X'].mean(), line_dash='dash', annotation_text= f"{df['X'].mean():.0f}")
fig.add_vline(x=df['Y'].mean(), line_dash='dash', annotation_text=f"{df['Y'].mean():.0f}")

fig.show()

密谋

My issue is that the horizontal line is plotted also plotted on the marginal x plot and the vertical line is also plotted on the marginal y plot. Is there a way to prevent the horizontal line to be plotted on the marginal x plot and the vertical line to be plotted on the marginal y plot?我的问题是水平线也绘制在边缘 x plot 上,垂直线也绘制在边缘 y plot 上。有没有办法防止水平线绘制在边缘 x plot 和垂直线上绘制在边缘 y plot 上?

You can define row and column of the panel grid when plotting hline/vline:您可以在绘制 hline/vline 时定义面板网格的行和列:

fig.add_hline(y=df['X'].mean(), line_dash='dash', row=1, annotation_text= f"{df['X'].mean():.0f}")
fig.add_vline(x=df['Y'].mean(), line_dash='dash', col=1, annotation_text=f"{df['Y'].mean():.0f}")

Sample output:样本 output: 在此处输入图像描述

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

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