1

Here I want to plot the fault for every every sub plots according to there unit. how to use "facet_row" for "figline.add_scattergl(x=df2.DateTime, y=df2.Value.where(df2.DataType=="Fault"), name="Fault", line={'color': 'red'})" this line.

import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

df2 = pd.DataFrame([
        dict(DateTime='2022-01-01 00:00:00', Value=90, Point="A", Unit='%', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:15:00', Value=80, Point="A", Unit='%', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:30:00', Value=85, Point="A", Unit='%', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:45:00', Value=92, Point="A", Unit='%', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 01:00:00', Value=100, Point="A", Unit='%', DataType='Fault',Type='Demo'),
        dict(DateTime='2022-01-01 01:15:00', Value=72, Point="A", Unit='%', DataType='Fault',Type='Demo'),
        dict(DateTime='2022-01-01 00:00:00', Value=22, Point="B", Unit='°C', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:15:00', Value=22, Point="B", Unit='°C', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:30:00', Value=23, Point="B", Unit='°C', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:45:00', Value=20, Point="B", Unit='°C', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 01:00:00', Value=21, Point="B", Unit='°C', DataType='Fault', Type='Demo'),
        dict(DateTime='2022-01-01 01:15:00', Value=23, Point="B", Unit='°C', DataType='Fault', Type='Demo'),    
        dict(DateTime='2022-01-01 00:00:00', Value=24, Point="C", Unit='°C', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:15:00', Value=24, Point="C", Unit='°C', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:30:00', Value=24, Point="C", Unit='°C', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:45:00', Value=24, Point="C", Unit='°C', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 01:00:00', Value=24, Point="C", Unit='°C', DataType='Fault', Type='Demo'),
        dict(DateTime='2022-01-01 01:15:00', Value=24, Point="C", Unit='°C', DataType='Fault', Type='Demo'),
        dict(DateTime='2022-01-01 00:00:00', Value=60, Point="D", Unit='Pa', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:15:00', Value=58, Point="D", Unit='Pa', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:30:00', Value=62, Point="D", Unit='Pa', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:45:00', Value=61, Point="D", Unit='Pa', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 01:00:00', Value=64, Point="D", Unit='Pa', DataType='Fault', Type='Demo'),
        dict(DateTime='2022-01-01 01:15:00', Value=59, Point="D", Unit='Pa', DataType='Fault', Type='Demo'),
        dict(DateTime='2022-01-01 00:00:00', Value=0, Point="E", Unit='Binary', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:15:00', Value=0, Point="E", Unit='Binary', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:30:00', Value=1, Point="E", Unit='Binary', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 00:45:00', Value=1, Point="E", Unit='Binary', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 01:00:00', Value=1, Point="E", Unit='Binary', DataType='Fault', Type='Demo'),
        dict(DateTime='2022-01-01 01:15:00', Value=1, Point="E", Unit='Binary', DataType='Fault', Type='Demo'),
        dict(DateTime='2022-01-01 01:30:00', Value=0, Point="E", Unit='Binary', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 01:45:00', Value=0, Point="E", Unit='Binary', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 02:00:00', Value=1, Point="E", Unit='Binary', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 02:15:00', Value=1, Point="E", Unit='Binary', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 02:30:00', Value=1, Point="E", Unit='Binary', DataType='Normal', Type='Demo'),
        dict(DateTime='2022-01-01 02:45:00', Value=1, Point="E", Unit='Binary', DataType='Normal', Type='Demo'),
    ])

figline = px.line(df2, x="DateTime", y="Value", color="Point", line_group="Unit", hover_name="Point", facet_row="Unit",
        line_shape="spline", render_mode="svg")
figline.update_yaxes(autorange=True)
figline.update_layout(hovermode="x unified")
figline.update_yaxes(matches=None)
#figline = highLights(fig = figline, dfChart=df2, variable = 'DataType', level = 'Fault', mode = 'Fault',
 #          fillcolor = 'rgba(200,0,200,0.2)', layer = 'below')
figline.add_scattergl(x=df2.DateTime, y=df2.Value.where(df2.DataType=="Fault"), name="Fault", line={'color': 'red'})
#figline.add_trace(go.Scatter(x=df2.DateTime, y=df2.Value.where(df2.DataType=="Fault"), name="Fault", line={'color': 'Gray'}))

figline.show()

The graph is supposed to like this: all the fault is over lapped on each line here the fault line is plotted in one subplot

Sartaj0111
  • 11
  • 1

2 Answers2

0

You can select in which of the plots the curve will be added with the argument yaxis="y4" (for example for the top graph, and "y1" for the bottom). But currently df2.Value.where(df2.DataType=="Fault") generates a "single curve" for all the points, you'd have to split it beforehand and then add each one with something like:

dfs = <list of dataframes containing each DateTime/Value>
for i,df in enumerate(dfs):
  figline.add_scattergl(x=df.DateTime, y=df.Value, name="Fault", line={'color': 'red'}, yaxis=f"y{i+1}")

PS: Since these are only 2 points, they will be plotted as straight lines, which won't be on top of the curves you plotted with line_shape="spline"

Filipe
  • 532
  • 4
  • 16
0

Thanks Filipe, can you please check below code if it is correct per your above suggestion? It is throwing error - AttributeError: 'str' object has no attribute 'DateTime'

df3 = df2.query("DataType== 'Fault'")
dfs = df3.groupby('Unit').agg(lambda x: list(x))

for i,df in enumerate(dfs):        
    figline.add_scattergl(x=df.DateTime, y=df.Value, name="Fault", line={'color': 'red'}, yaxis=f"y{i+1}")
  • Answers are meant to answer the question that was asked. If you need help with another error in your code, either edit your question or add this as a comment to the other person's answer. – Roni Jun 16 '22 at 11:00