[英]Plotly animation line chart change color of the annotation value
I have a data frame 'df_vcm_set_funct_mode1' I am dding two columns to it and ploting using plotly animation chart.我有一个数据框 'df_vcm_set_funct_mode1' 我正在添加两列并使用 plotly animation 图表进行绘图。 Please see my code below.请在下面查看我的代码。
df_vcm_set_funct_mode1['SPEC_MIN'] = -0.01
df_vcm_set_funct_mode1['SPEC_MAX'] = 0.01
VCM_DELTA_FUN = px.line(df_vcm_set_funct_mode1, x = "Temp(deg)", y=["VCM_DELTA_10ms",'VCM_DELTA_20ms','SPEC_MIN','SPEC_MAX'],
color = "Device_ID",animation_frame="Supply[V]",title="VCM SETTLING 10ms 20ms",markers=True)
VCM_DELTA_FUN.add_annotation(x=-40, y= -0.01,
text="SPEC_MIN",
showarrow=True,
arrowhead=1)
VCM_DELTA_FUN.add_annotation(x=-40, y= 0.01,
text="SPEC_MAX",
showarrow=True,
arrowhead=1)
VCM_DELTA_FUN.update_layout(xaxis_title = "Voltage",yaxis_title = "VCM_DELTA 20ms & 10ms")
VCM_DELTA_FUN.show()
When I plot the graph I am getting same color for both of the annotated things(SPEC_MIN and SPEC_MAX).Please see the graph below.当我 plot 时,我得到两个带注释的东西(SPEC_MIN 和 SPEC_MAX)的颜色相同。请参见下图。
I need spec_max and Spec_min in two different colors.我需要两个不同的 colors 中的 spec_max 和 Spec_min。 May I know how to do the same我可以知道怎么做吗
Device_ID Die_Version Temp(deg) Supply[V] VCM_10ms VCM_20ms VCM_5S VCM_DELTA_10ms VCM_DELTA_20ms
FFK_571 0x81 -40 2.5 1.286 1.284 1.282 -0.003 -0.001
SFK_619 0x81 -40 2.5 1.263 1.258 1.236 -0.027 -0.022
TTK_538 0x81 -40 2.5 1.279 1.279 1.273 -0.006 -0.006
FFK_TN_631 0x81 -40 2.5 1.283 1.282 1.279 -0.004 -0.003
FFK_TN_631 0x81 -40 2.7 1.287 1.286 1.283 -0.004 -0.003
IIUC, we have data that looks something like this: IIUC,我们的数据看起来像这样:
import pandas as pd
import numpy as np
import plotly.express as px
data = {'Device_ID':np.repeat(['A','B'],8),
'Temp':np.tile([*range(-40,120,20)],2),
'Supply': [2.5]*16,
'VCM_DELTA 20ms':np.random.uniform(-0.03,0.03,16),
'VCM_DELTA 10ms':np.random.uniform(-0.03,0.03,16)}
df = pd.DataFrame(data)
print(df)
Device_ID Temp Supply VCM_DELTA 20ms VCM_DELTA 10ms
0 A -40 2.5 0.025515 -0.009489
1 A -20 2.5 -0.003601 -0.028498
2 A 0 2.5 0.006799 0.005940
3 A 20 2.5 0.026511 0.002165
4 A 40 2.5 0.023773 -0.021034
5 A 60 2.5 -0.017705 -0.026885
6 A 80 2.5 0.016171 -0.001522
7 A 100 2.5 -0.026219 -0.026108
8 B -40 2.5 0.028767 -0.023051
9 B -20 2.5 -0.004207 -0.024583
10 B 0 2.5 0.026277 -0.021089
11 B 20 2.5 -0.018630 0.010538
12 B 40 2.5 0.009662 -0.029476
13 B 60 2.5 0.006762 0.027054
14 B 80 2.5 0.005418 -0.007464
15 B 100 2.5 -0.001756 -0.019856
Plot Plot
With SPEC_MIN
(at y=-0.01
) and SPEC_MAX
(at y=0.01
) added in different colors.在不同的 colors 中添加了SPEC_MIN
(在y=-0.01
处)和SPEC_MAX
(在y=0.01
处)。 We could do something like this:我们可以这样做:
fig = px.line(df, x = "Temp", y=['VCM_DELTA 20ms','VCM_DELTA 10ms'],
color = 'Device_ID', animation_frame='Supply', markers=True)
d = {'SPEC_MIN': [-0.01,'Green'],
'SPEC_MAX': [0.01,'Purple']}
min_temp, max_temp = df.Temp.agg(['min','max'])
for k,v in d.items():
fig.add_shape(type='line',
x0=min_temp,
x1=max_temp,
y0 = v[0],
y1= v[0],
line=dict(color=v[1],dash="dash"),
)
fig.add_annotation(x=min_temp, y=v[0],
text=k,
showarrow=True,
arrowhead=5,
ay=-50,
ax=-50)
fig.update_layout(xaxis_title = "Voltage",
yaxis_title = "VCM_DELTA 20ms & 10ms")
fig.show()
Alternatively, instead of fig.add_shape
, you could also use fig.add_hline
, if you don't mind the fact that these lines will cross the entire x-axis.或者,如果您不介意这些线将穿过整个 x 轴,您也可以使用fig.add_hline
代替fig.add_shape
。 Eg:例如:
for k,v in d.items():
fig.add_hline(type='line', y=v[0],
line=dict(color=v[1],dash="dash"),
)
fig.add_annotation(x=min_temp, y=v[0],
text=k,
showarrow=True,
arrowhead=5,
ay=-50,
ax=-50)
NB The problem with your initial approach (adding columns SPEC_MIN
and SPEC_MIN
) is that you were simply adding two lines per each ID
in Device_ID
.注意您最初方法的问题(添加列SPEC_MIN
和SPEC_MIN
)是您只是在Device_ID
中为每个ID
添加两行。 The reason why both lines are purple has to do with the fact that for each ID
, you are adding the exact same lines.两条线都是紫色的原因与以下事实有关,即对于每个ID
,您都添加了完全相同的线。 In the end, then, the two lines that we see are matching the color "purple" for the last unique value in Device_ID
, as they overwrite the previous lines for "blue, red, green".最后,我们看到的两行与Device_ID
中最后一个唯一值的颜色“紫色”相匹配,因为它们覆盖了前面的“蓝色、红色、绿色”行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.