Search code examples
pythonpandasplotlyfacet

Trying to create a facet graph from aggregated data using a pandas Dataframe using Plotly


I want to use Plotly to create a facet graph, however the code I'm using keeps returning an attribute error and I am not sure how to address the Dataframe in the code

So I have been able to aggregate the data for specific details and I can use a loop to print out several graphs using Matplotlib.

The data is as follows:

Metric  Count Collection_Start_Date Collection_End_Date  frequency  \
1   PAIRED TRADES     15            2019-06-25          2019-02-01          7   
3   PAIRED TRADES     19            2019-06-25          2019-01-01          7   
5   PAIRED TRADES     90            2019-06-25          2019-03-01          7   
7   PAIRED TRADES     19            2019-06-25          2019-02-01          7   
9   PAIRED TRADES     86            2019-06-25          2019-01-01          7   
11  PAIRED TRADES     88            2019-06-25          2019-03-01          7   
13  PAIRED TRADES     57            2019-06-25          2019-02-01          7   
15  PAIRED TRADES      9            2019-06-25          2019-01-01          7   
17  PAIRED TRADES     98            2019-06-25          2019-03-01          7   
19  PAIRED TRADES     89            2019-06-25          2019-02-01          7   
21  PAIRED TRADES     28            2019-06-25          2019-01-01          7   
23  PAIRED TRADES     92            2019-06-25          2019-03-01          7   
25  PAIRED TRADES     57            2019-06-25          2019-02-01          7   
27  PAIRED TRADES     87            2019-06-25          2019-01-01          7   
29  PAIRED TRADES     55            2019-06-25          2019-03-01          7   
31  PAIRED TRADES     95            2019-06-25          2019-02-01          7   
33  PAIRED TRADES     17            2019-06-25          2019-01-01          7   
35  PAIRED TRADES     39            2019-06-25          2019-03-01          7   

df = pd.read_csv(...)

df = df.query('Metric == "PAIRED TRADES"')

print(df)

fig = px.df.tips()

I keep getting an attribute error stating:

module 'plotly_express' has no attribute 'df'

and it's referring to the fig =px.df.tips() line


Solution

  • You misinterpreted the facet plot docs as tips is a built-in dataset maintained inside the plotly_express module, likely included for demo purposes.

    import plotly.express as px
    
    tips = px.data.tips()     # BUILT-IN DATASET
    fig = px.scatter(tips, x="total_bill", y="tip", color="smoker", facet_col="sex")
    fig.show()
    

    For your needs, you do not need such as demo dataset. Simply assign your data frame inside the data argument. Below demonstrates with a scatter plot:

    df = df.query('Metric == "PAIRED TRADES"')
    
    fig = px.scatter(df, x="Collection_End_Date", y="Count", color="smoker", facet_col="Entity")
    fig.show()