Home > Software design >  how can def. a function for plotting
how can def. a function for plotting

Time:09-11

I plot the normal distribution with Python Plotly for several data frames with different columns. It would be effective if a function could be defined for plotting. Otherwise, I plot different values each time but with the same parameters.

My current code, which actually works:

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')

this repeats for at least 3 more

CodePudding user response:

I think that the below function will solve your problem:

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

You can use the same logic to improve it...

Regards, Leonardo

  • Related