Home > Enterprise >  Invalid value of type 'datetime.date' received for the 'name' property of frame
Invalid value of type 'datetime.date' received for the 'name' property of frame

Time:09-13

I am trying to plot an animated scatter plot using plotly. Using a date-wise animation frame, but I am getting an error Invalid value of type 'datetime.date'.

fig = px.scatter(df1,x=df1['MeterReading_DateTime'], y=df1['ACT_IMP_TOT'], color=df1['MeterReading_DateTime'],animation_group=df1['MeterReading_DateTime'],animation_frame=df1['MeterReading_DateTime'],size=df1['ACT_IMP_TOT'],size_max=55,title='')

Here I am sharing the data frame. dataframe

CodePudding user response:

  • the error is exactly what it says.... for color, animation_group and animation_frame the date needs to be expressed as as string
  • have simulated data implied by your code and image of data
  • created an additional column to be able to use code as you want
  • cleaned up a few other issues to make axes change when you switch between frames
import plotly.express as px
import pandas as pd
import numpy as np

df1 = pd.DataFrame(
    {"MeterReading_DateTime": pd.date_range("15-aug-2022", "today", freq="D")}
).assign(ACT_IMP_TOT=lambda d: np.random.uniform(9, 15, len(d)))
# column uses for color / animation needs to be a string
df1["ReadingDate_str"] = df1["MeterReading_DateTime"].dt.strftime("%Y-%m-%d")

# use data as str for color and animation
fig = px.scatter(
    df1,
    x=df1["MeterReading_DateTime"],
    y=df1["ACT_IMP_TOT"],
    color=df1["ReadingDate_str"],
    animation_group=df1["ReadingDate_str"],
    animation_frame=df1["ReadingDate_str"],
    size=df1["ACT_IMP_TOT"],
    size_max=55,
    title="",
)

# need to define xaxis for each frame
for fr in fig.frames:
    fr.update(
        layout={
            "xaxis": {
                "range": [fr.data[0].x[0], fr.data[0].x[0]],
                "tickformat": "%d-%b",
            }
        }
    )

# yaxis needs to be approriate range across all frames
fig.update_layout(
    yaxis_range=[df1["ACT_IMP_TOT"].min(), df1["ACT_IMP_TOT"].max()],
    xaxis_tickformat="%d-%b",
    showlegend=False,
)
  • Related