Plotly:将分位数间距添加到散点图 [英] Plotly: Add Inter quantile range to the scatter plot

查看:59
本文介绍了Plotly:将分位数间距添加到散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 plotly express 创建了一个散点图.我想在同一个图中添加分位数范围.我试图通过创建一个线图,然后将宽度增加到分位数间范围并降低不透明度以获得预期的图.但它不起作用.

I have created a scatter plot using plotly express. I want to add inter quantile range to the same plot. I tried to that by creating a line plot, then increasing width as much as inter quantile range and decreasing the opacity to get the expected plot. But it isn't worked.

代码:

sample = {'columns': ['MM_ln_MM_sn', 'MM_l_MM_s', 'MM_l', 'MM_s_cc', 'date1'],
 'data': [[45, 69880, 99739, 10.33251321021105, '13/06/06'],
  [34, 61116, 91198, 10.4397740050252, '20/06/06'],
  [25, 57215, 88635, 10.73127747285588, '27/06/06'],
  [27, 59326, 90104, 10.52239316239316, '04/07/06'],
  [37, 71884, 101019, 9.317713730155685, '11/07/06'],
  [31, 79271, 105068, 7.955260333789735, '18/07/06'],
  [36, 72907, 96891, 7.598650339791215, '25/07/06'],
  [40, 70609, 95785, 8.256943920605298, '01/08/06'],
  [44, 74318, 100994, 8.630300520548566, '08/08/06'],
  [44, 67890, 94115, 8.369850028883564, '15/08/06'],
  [45, 60305, 87386, 8.845057173932213, '22/08/06'],
  [34, 49529, 80809, 10.16961275493119, '29/08/06'],
  [38, 58222, 87652, 9.196932480828005, '05/09/06'],
  [30, 45940, 76571, 9.86473865575988, '12/09/06'],
  [25, 41593, 72225, 9.400586769453618, '19/09/06'],
  [12, 35324, 68325, 10.21902104744888, '26/09/06'],
  [13, 32453, 66119, 10.19980912850499, '03/10/06'],
  [7, 23064, 62270, 11.72677213164278, '10/10/06'],
  [11, 21829, 64602, 12.9664266767715, '17/10/06'],
  [12, 10179, 54019, 13.22082762863467, '24/10/06']],
 'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]}

df = pd.DataFrame(index=sample['index'], columns=sample['columns'], data=sample['data'])

fig1 = px.scatter(df, x="MM_ln_MM_sn", y="MM_l_MM_s",trendline="ols",
             size="MM_l", color="MM_s_cc",
                 hover_name="date1", log_x=True, size_max=30)


x = [df["MM_ln_MM_sn"].values[-1]]
y = [df["MM_l_MM_s"].values[-1]]
fig1.add_traces(go.Scatter(x=x, y=y,
                  marker=dict(color='black',size=16),
                  showlegend=False
                    )
                )

xval = list(range(df["MM_ln_MM_sn"].min()-20 ,df["MM_ln_MM_sn"].max()+20))
fig2 = px.line(x=xval,y=[np.median(df["MM_l_MM_s"])]*len(xval))

yval = list(range(df["MM_l_MM_s"].min()-int(5*1e4) ,df["MM_l_MM_s"].max()+int(5*1e4)))
fig3 = px.line(y=yval,x=[np.median(df["MM_ln_MM_sn"])]*len(yval))

fig = go.Figure(data=fig1.data + fig2.data + fig3.data)

fig.update_layout(
     title='Dry Powder Net Indicator',
    xaxis_title='MM Net Number of Traders',
    yaxis_title='MM Net OI',
    showlegend=False)

fig.update_traces(boxmean= True, selector=dict(type='box'))


fig.show()

IQR 值:

xq2 = np.percentile(df["MM_ln_MM_sn"], 25)
xq3 = np.percentile(df["MM_ln_MM_sn"], 75)
yq2 = np.percentile(df["MM_l_MM_s"], 25)
yq3 = np.percentile(df["MM_l_MM_s"], 75)

输出:(需要添加IQR)

预期:

代码-V1:

fig.add_hline(y=np.median(df["MM_l_MM_s"]), line_dash="dot", row="all", col="all",
              annotation_text="Median", 
              annotation_position="bottom right")

fig.add_vline(x=np.median(df["MM_ln_MM_sn"]), line_dash="dot", row="all", col="all",
              annotation_text="Median", 
              annotation_position="bottom right")

fig.add_vrect(x0=xq2, x1=xq3, row="all", col=1,
              annotation_text="decline", annotation_position="top left",
              fillcolor="green", opacity=0.25, line_width=0)


fig.add_vrect(x0=yq2, x1=yq3, row="all", col="all",
              annotation_text="decline", annotation_position="top left",
              fillcolor="green", opacity=0.25, line_width=0)

输出:

推荐答案

我将此添加到您的代码末尾.

I added this to the end of your code.

xq2 = np.percentile(df["MM_ln_MM_sn"], 25)
xq3 = np.percentile(df["MM_ln_MM_sn"], 75)
yq2 = np.percentile(df["MM_l_MM_s"], 25)
yq3 = np.percentile(df["MM_l_MM_s"], 75)

print(xq2, xq3, yq2, yq3)

fig.add_hline(y=np.median(df["MM_l_MM_s"]), line_dash="dot", row="all", col="all",
              annotation_text="Median", 
              annotation_position="bottom right")

fig.add_vline(x=np.median(df["MM_ln_MM_sn"]), line_dash="dot", row="all", col="all",
              annotation_text="Median", 
              annotation_position="bottom right")

fig.add_vrect(x0=xq2, x1=xq3, row="all", col=1,
              annotation_text="decline", annotation_position="top left", layer='below',
              fillcolor="#CCCCCC", opacity=1, line_width=0)


fig.add_hrect(y0=yq2, y1=yq3, row="all", col="all",
              annotation_text="decline", annotation_position="top left", layer='below',
              fillcolor="#CCCCCC", opacity=1, line_width=0)

这是我生成的输出.

评论我发布此信息是因为我们认为在使用关键字 annotation_textannotation_position 时可能存在错误.

Comment I am posting this, because we thought there could be a bug when using the keywords annotation_text and annotation_position.

在我的小测试中,我看不到任何奇怪的东西.一切都按预期工作.

In my little test I couldn't see any thing strange. Everything is working like expected.

这篇关于Plotly:将分位数间距添加到散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆