-1

I created this graph below to show the revenue and expenses of a company using Plotly. enter image description here What I want to do now is to find the net profit by subtracting the two values in each month and adding a scatter plot using this data. Entering my data into the graph was a little bit complicated and I can't subtract the values before putting them into the graph. So what I want is to be able to get the exact value of each individual column. I can see the values when I hover onto the columns but I couldn't figure out how to get them. Is there any way that I can achieve this?

  • How are you plotting the data then if you don't have the values? Please post a working code so we can better diagnose the issue. – Mohammad Aug 03 '21 at 15:32

1 Answers1

0
  • it's much simpler to do data prep in pandas and then use plotly to generate required figure
  • have shown that I have data that first is resampled, then Profit is calculated from resampled data
  • plot is then straight forward
  • this technique can easily applied to your use case using the parameters you pass to x and y parameters to generate your traces
import pandas as pd
import numpy as np
import plotly.express as px

d = pd.date_range("30-jun-2020", "now")
df = pd.DataFrame(
    {
        "date": d,
        "Revenue": np.random.uniform(1, 15, len(d)),
        "Expenses": np.random.uniform(1, 10, len(d)),
    }
)

dfp = (
    df.resample("1M", on="date")
    .sum()
    .assign(Profit=lambda d: d["Revenue"] - d["Expenses"])
)

fig = px.bar(dfp, x=dfp.index, y=["Revenue", "Expenses"]).update_layout(
    barmode="group"
).add_traces(
    px.line(dfp, x=dfp.index, y="Profit")
    .update_traces(
        showlegend=True, name="Profit", line={"color": "yellow", "dash": "dot"},
        mode="lines+markers",
        marker={"symbol":"square", "size":10}
    )
    .data
)

fig.show()

extract data from a figure

  • use to_json() method to get JSON
  • can then simply use json normalize to load into a dataframe
  • use standard techniques for extracting embedded list
import json

df = pd.json_normalize(json.loads(fig.to_json())["data"]).loc[:,["name","x","y"]].set_index("name")

df["x"].apply(pd.Series).T.join(df["y"].apply(pd.Series).T, lsuffix="_x", rsuffix="_y", how="outer")

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30