如何定义绘图函数



我用Python Plotly为具有不同列的几个数据帧绘制正态分布。如果可以为绘图定义一个函数,那么它将是有效的。否则,我每次绘制不同的值,但使用相同的参数。

我目前的代码,它实际上是有效的:

fig2 = ff.create_distplot(
[fscl_without_outl_P2["P2"].tolist()],
group_labels=["LP 2.4kW"],
show_hist=False,
curve_type = "normal",
bin_size = fsc_hist_parameter["P2"]["Range"]
).add_traces(
px.histogram(without_outl_P2, x="P2", nbins= fsc_hist_parameter["P2"]["bins"], opacity=0.8) 
)
.update_traces(yaxis="y3", name="histogram")
.data  
).add_vline(y0=0, x=OEG, line_dash="longdash",
annotation_text="UCL: " + "<br>" + str(format(OEG)), annotation_position="bottom"
).add_vline(y0=0, x=UEG, line_dash="longdash",
annotation_text="LCL: " + "<br>" + str(format(UEG)), annotation_position="bottom"
).add_vline(y0=0, x=df_12m["HP LPIFS_Wert P2"].mean(), line_dash="dashdot",
annotation_text="µ: " + "<br>" + str("{:.3f}".format(df_12m["HP LPIFS_Wert P2"].mean())),
annotation_position="bottom"
).add_vline(y0=0, x=Sigma_limit["P2_3Sigma"]["+3Sigma_Grenze"], line_dash="longdash",
annotation_text="+3σ-limit: " + "<br>" + str("{:.3f}".format(Sigma_limit["P2_3Sigma"] 
["+3Sigma_Grenze"])), annotation_position="bottom right", line_color="red"
).add_vline(y0=0, x=Sigma_limit["P2_3Sigma"]["-3Sigma_Grenze"], line_dash="longdash",
annotation_text="-3σ-limit: " + "<br>" + str("{:.3f}".format(Sigma_limit["P2_3Sigma"]
["-3Sigma_Grenze"])), annotation_position="bottom", line_color="red"
).update_layout(yaxis3={"overlaying": "y", "side": "right"}, showlegend=True,
title_text="Normal distribution", bargap=0.2,
yaxis3_title='Frequency', xaxis_title='Laser Power')

这种情况至少重复3次

我认为以下函数将解决您的问题:

def plot_chart(fscl_without_outl_P2, without_outl_P2, fsc_hist_parameter, OEG, UEG, df_12m, Sigma_limit):
fig = ff.create_distplot(
[fscl_without_outl_P2["P2"].tolist()],
group_labels=["LP 2.4kW"],
show_hist=False,
curve_type = "normal",
bin_size = fsc_hist_parameter["P2"]["Range"]
).add_traces(
px.histogram(without_outl_P2, x="P2", nbins= fsc_hist_parameter["P2"]["bins"], opacity=0.8) 
)
.update_traces(yaxis="y3", name="histogram")
.data  
).add_vline(y0=0, x=OEG, line_dash="longdash",
annotation_text="UCL: " + "<br>" + str(format(OEG)), annotation_position="bottom"
).add_vline(y0=0, x=UEG, line_dash="longdash",
annotation_text="LCL: " + "<br>" + str(format(UEG)), annotation_position="bottom"
).add_vline(y0=0, x=df_12m["HP LPIFS_Wert P2"].mean(), line_dash="dashdot",
annotation_text="µ: " + "<br>" + str("{:.3f}".format(df_12m["HP LPIFS_Wert P2"].mean())),
annotation_position="bottom"
).add_vline(y0=0, x=Sigma_limit["P2_3Sigma"]["+3Sigma_Grenze"], line_dash="longdash",
annotation_text="+3σ-limit: " + "<br>" + str("{:.3f}".format(Sigma_limit["P2_3Sigma"] 
["+3Sigma_Grenze"])), annotation_position="bottom right", line_color="red"
).add_vline(y0=0, x=Sigma_limit["P2_3Sigma"]["-3Sigma_Grenze"], line_dash="longdash",
annotation_text="-3σ-limit: " + "<br>" + str("{:.3f}".format(Sigma_limit["P2_3Sigma"]
["-3Sigma_Grenze"])), annotation_position="bottom", line_color="red"
).update_layout(yaxis3={"overlaying": "y", "side": "right"}, showlegend=True,
title_text="Normal distribution", bargap=0.2,
yaxis3_title='Frequency', xaxis_title='Laser Power')
return fig

你可以用同样的逻辑来改进它…

谨致问候,Leonardo

def plot_for_different_Power(df, col_datetime, col_1,col_rol_1, col_2, 
col_rol_2,LCL, UCL):
fig = make_subplots(rows=2, cols=1)
fig.append_trace(go.Scatter(y=df[col_1],name=col_1), row=1, col=1)
fig.append_trace(go.Scatter(y=df[col_rol_1],name=col_rol_1), row=1, 
col=1)
fig.append_trace(go.Scatter(y=df[col_2],name=col_2), row=2, col=1)
fig.append_trace(go.Scatter(y=df[col_rol_2], name=col_rol_2), row=2, 
col=1)

fig.add_hline(y = LCL, y1 = LCL, row = 'all', col = 1,
annotation_text = 'LCL: ' + str(format(LCL)),
annotation_position = 'top right',
annotation_font_size = 10, line_dash='solid',
line_color = 'red', line_width = 3, opacity = 1)
fig.add_hline(y = UCL, y1 = UCL, row = 'all', col = 1,
annotation_text = 'UCL: ' + str(format(UCL)),
annotation_position = 'top right',
annotation_font_size = 10, line_dash='solid',
line_color = 'red', line_width = 3, opacity = 1)
fig.add_hrect(y0=0.0, y1=0.2, line_width=0, fillcolor='red',opacity=0.5)
fig.add_hrect(y0=-0.194, y1=-0.4, line_width=0, fillcolor='red', opacity=0.5)
fig.add_hrect(y0=0, y1=-0.194, line_width=0, fillcolor='green', opacity=0.2)
fig.update_traces(x=df[col_datetime])
fig.update_traces(mode="lines+markers", marker_size=5, line_width=1, 
customdata=df)
return fig`

我找到了一个通用的解决方案,可以以各种方式统一使用

最新更新