gpt4 book ai didi

python - 破折号在子图之间动态注释线图

转载 作者:行者123 更新时间:2023-12-05 04:20:36 26 4
gpt4 key购买 nike

我有一个类似于下面的数据集。请注意,单个 ID 有多个值。

import pandas as pd
import numpy as np
import random

df = pd.DataFrame({'DATE_TIME':pd.date_range('2022-11-01', '2022-11-05 23:00:00',freq='h'),
'SBP':[random.uniform(110, 160) for n in range(120)],
'DBP':[random.uniform(60, 100) for n in range(120)],
'ID':[random.randrange(1, 100) for n in range(120)],
'TIMEINTERVAL':[random.randrange(1, 200) for n in range(120)]})

df['VISIT'] = df['DATE_TIME'].dt.day

df['MODE'] = np.select([df['VISIT']==1, df['VISIT'].isin([2,3])], ['New', 'InProgress'], 'Done')

我使用以下 DASH 代码制作 slider :

app = Dash(__name__)


app.layout = html.Div([
html.H4('Interactive Scatter Plot with ABPM dataset'),
dcc.Graph(id="scatter-plot"),
html.P("Filter by time interval:"),
dcc.Dropdown(df.ID.unique(), id='pandas-dropdown-1'), # for choosing ID,
dcc.RangeSlider(
id='range-slider',
min=0, max=600, step=10,
marks={0: '0', 50: '50', 100: '100', 150: '150', 200: '200', 250: '250', 300: '300', 350: '350', 400: '400', 450: '450', 500: '500', 550: '550', 600: '600'},
value=[0, 600]
),
html.Div(id='dd-output-container')
])


@app.callback(
Output("scatter-plot", "figure"),
Input("pandas-dropdown-1", "value"),
Input("range-slider", "value"),
prevent_initial_call=True)

def update_bar_chart(value,slider_range):
low, high = slider_range
df1 = df.query("ID == @value & TIMEINTERVAL > @low & TIMEINTERVAL < @high").copy()

if df1.shape[0] != 0:
fig = px.scatter(df1, x="DATE_TIME", y=["SBP","DBP"],
hover_data=['TIMEINTERVAL'],facet_col='VISIT',
facet_col_wrap=2,
symbol='MODE')

fig.update_xaxes(matches= None, showticklabels=True)

return fig
else:
return dash.no_update


app.run_server(debug=True, use_reloader=False)

如果 df1 Visit 列的值大于 1,那么我想用箭头注释子图以明确阅读。为此,我在 update_bar_charts 函数中编写了以下脚本,但它没有编译。

def update_bar_chart(value,slider_range):
low, high = slider_range
df1 = df.query("ID == @value & TIMEINTERVAL > @low & TIMEINTERVAL < @high").copy()

if df1.shape[0] != 0:
fig = px.scatter(df1, x="DATE_TIME", y=["SBP","DBP"],
hover_data=['TIMEINTERVAL'],facet_col='VISIT',
facet_col_wrap=2,
symbol='MODE')

fig.update_xaxes(matches= None, showticklabels=True)

if df1.VISIT!=1:
fig.add_annotation(
xref="x domain",
yref="y domain",
# The arrow head will be 25% along the x axis, starting from the left
x=0.25,
# The arrow head will be 40% along the y axis, starting from the bottom
y=0.4,
arrowhead=2,
)

return fig
else:
return dash.no_update


app.run_server(debug=True, use_reloader=False)

我有的是enter image description here :

我想实现的是:

enter image description here

我怎样才能添加这些箭头以便更容易阅读图表?箭头数量应动态变化,因为每个 ID 的访问次数不同。

最佳答案

添加循环遍历子图行的注释。使用注释的 'xref'/'yref' 属性的 'x/y* domain' 值,将坐标指定为与 x/y 域(子图框架的宽度/高度)的比率。还可以使用 'ax'/'ay' 属性来指定箭头的起点。

这是一个例子。

n_plots = len(df1['VISIT'].unique())
n_rows = (n_plots+1)//2
row_spacing = 1/((1/0.5+1) * n_rows - 1) # 50% of y domain
col_spacing = 0.1
col_spacing_in_x_domain = 1/((1/col_spacing-1)/2)
row_spacing_in_y_domain = 1/((1/row_spacing+1)/n_rows - 1)

fig = px.scatter(df1,
facet_col='VISIT',
facet_col_wrap=2,
facet_row_spacing=row_spacing, facet_col_spacing=col_spacing,
...
)
fig.update_xaxes(matches= None, showticklabels=True)

for i in range(n_rows):
# A row number 1 is the bottom one.
trace = next(fig.select_traces(row=n_rows-i, col=1))
xref, yref = trace.xaxis + ' domain', trace.yaxis + ' domain'
if i*2+1 < n_plots:
fig.add_annotation(
xref=xref, yref=yref, axref=xref, ayref=yref,
ax=1, ay=0.5,
x=1 + col_spacing_in_x_domain, y=0.5,
arrowhead = 2,
)
if i*2+2 < n_plots:
fig.add_annotation(
xref=xref, yref=yref, axref=xref, ayref=yref,
ax=1 + col_spacing_in_x_domain, ay=0.5,
x=1, y=-row_spacing_in_y_domain - 0.5,
arrowhead = 2,
)

关于python - 破折号在子图之间动态注释线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74432287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com