다음과 같은 데이터 프레임이 있습니다.
df =
time_id gt_class num_missed_base num_missed_feature num_objects_base num_objects_feature
5G21A6P00L4100023:1566617404450336 CAR 11 4 27 30
5G21A6P00L4100023:1566617404450336 BICYCLE 4 6 27 30
5G21A6P00L4100023:1566617404450336 PERSON 2 3 27 30
5G21A6P00L4100023:1566617404450336 TRUCK 1 0 27 30
5G21A6P00L4100023:1566617428450689 CAR 25 14 60 67
5G21A6P00L4100023:1566617428450689 PERSON 7 6 60 67
5G21A6P00L4100023:1566617515950900 BICYCLE 1 1 59 65
5G21A6P00L4100023:1566617515950900 CAR 20 9 59 65
5G21A6P00L4100023:1566617515950900 PERSON 10 2 59 65
5G21A6P00L4100037:1567169649450046 CAR 8 0 29 32
5G21A6P00L4100037:1567169649450046 PERSON 1 0 29 32
5G21A6P00L4100037:1567169649450046 TRUCK 1 0 29 32
각각 time_id
은 기본 모델 num_missed_base
에서 누락 된 개체 수, 피처 모델에서 누락 된 num_missed_feature
개체 수 및 해당 시점에 기본 및 피처에 존재하는 개체 수를 보여줍니다 num_objects_base
.num_objects_feature
의 (plotly.graph_objs 및 FigureWidget)을 사용하여 산점도를 그려야합니다. time_id
사용자가 각 점 (각 점은 고유함을 나타냄) 위로 마우스를 가져 time_id
가면 다음에 대해 다음이 표시되도록합니다 time_id == 5G21A6P00L4100023:1566617404450336
.
hover_text
아래 코드 에는 무엇이 있어야 합니까?
import plotly.graph_objs as go
hover_text = ????
df_agg = df.groupby("time_id").sum().reset_index()
error_trace = go.Scattergl(
x=df_agg["num_missed_base"].tolist(),
y=df_agg["num_missed_feature"].tolist(),
text=hover_text,
mode="markers",
marker=dict(cmax=50, cmin=-50, opacity=0.3),
)
팬더 전문가라면 아래 코드를 좀 더 우아하고 효율적으로 만들 수있을 것입니다. 그러나 내 해결 방법도 작업을 수행합니다. 주요 과제는 소스 데이터 프레임을 다음과 같이 그룹화 된 버전으로 바꾸는 것입니다.
time_id gt_class num_missed_base base_str num_missed_feature feature_str
0 5G21A6P00L4100023:1566617404450336 CAR,BICYCLE,PERSON,TRUCK 18 11,4,2,1 13 11,4,2,1
1 5G21A6P00L4100023:1566617428450689 CAR,PERSON 32 25,7 20 25,7
2 5G21A6P00L4100023:1566617515950900 BICYCLE,CAR,PERSON 31 1,20,10 12 1,20,10
3 5G21A6P00L4100037:1567169649450046 CAR,PERSON,TRUCK 10 8,1,1 0 8,1,1
나쁜 소식은 이것이 충분하지 않다는 것입니다. 좋은 소식은 아래 스 니펫이 모든 것을 처리하고이 플롯을 제공한다는 것입니다.
여기에 표시되는 것은 각 타임 스탬프에 대한 관련 데이터를 그룹화하여 예를 들어 num_missed_feature
모든 클래스 의 합계 와 hoverinfo에서 각 기본 클래스의 수를 볼 수있는 플롯입니다. 조금 더 조정하면 합계도 포함 할 수 있습니다. 하지만 지금은이게 전부입니다.
import pandas as pd
import re
import plotly.graph_objects as go
smpl = {'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
'columns': ['time_id',
'gt_class',
'num_missed_base',
'num_missed_feature',
'num_objects_base',
'num_objects_feature'],
'data': [['5G21A6P00L4100023:1566617404450336', 'CAR', 11, 4, 27, 30],
['5G21A6P00L4100023:1566617404450336', 'BICYCLE', 4, 6, 27, 30],
['5G21A6P00L4100023:1566617404450336', 'PERSON', 2, 3, 27, 30],
['5G21A6P00L4100023:1566617404450336', 'TRUCK', 1, 0, 27, 30],
['5G21A6P00L4100023:1566617428450689', 'CAR', 25, 14, 60, 67],
['5G21A6P00L4100023:1566617428450689', 'PERSON', 7, 6, 60, 67],
['5G21A6P00L4100023:1566617515950900', 'BICYCLE', 1, 1, 59, 65],
['5G21A6P00L4100023:1566617515950900', 'CAR', 20, 9, 59, 65],
['5G21A6P00L4100023:1566617515950900', 'PERSON', 10, 2, 59, 65],
['5G21A6P00L4100037:1567169649450046', 'CAR', 8, 0, 29, 32],
['5G21A6P00L4100037:1567169649450046', 'PERSON', 1, 0, 29, 32],
['5G21A6P00L4100037:1567169649450046', 'TRUCK', 1, 0, 29, 32]]}
df = pd.DataFrame(index=smpl['index'], columns = smpl['columns'], data=smpl['data'])
df['base_str'] = df['num_missed_base'].astype(str)
df['feature_str'] = df['num_missed_base'].astype(str)
df2=df.groupby(['time_id'], as_index = False).agg({'gt_class': ','.join,
'num_missed_base':sum,
'base_str':','.join,
'num_missed_feature':sum,
'feature_str':','.join,})
col_elem=[]
row_elem=[]
for i in df2.index:
gt_class = df2['gt_class'].loc[i].split(',')
base_str = df2['base_str'].loc[i].split(',')
for j, elem in enumerate(gt_class):
new_elem = elem+": "+base_str[j]
row_elem.append(new_elem)
col_elem.append(row_elem)
row_elem=[]
df2['hover']=col_elem
df2['hover'] = df2['hover'].astype(str)
df2['hover2'] = df2['hover'].map(lambda x: x.lstrip('[]').rstrip(']'))
#df2['hover2'].apply(lambda x: x.str.replace(',','.'))
df2['hover2']=df2['hover2'].replace("'",'', regex=True)
df2['hover2']=df2['hover2'].replace(',','<br>', regex=True)
# plotly
fig = go.Figure()
fig.add_traces(go.Scatter(x=df2['num_missed_base'], y=df2['num_missed_feature'],
mode='markers', marker=dict(color='red',
line=dict(color='black', width=1),
size=14),
#hovertext=df2["hover"],
hovertext=df2['hover2'],
hoverinfo="text",
))
fig.update_xaxes(showspikes=True, linecolor='black', title='Base',
spikecolor='black', spikethickness=0.5, spikedash='solid')
fig.update_yaxes(showspikes=True, linecolor='black', title = 'Feature',
spikecolor='black', spikethickness=0.5, spikedash='solid')
fig.update_layout(
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)'
)
fig.show()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다