Returned a value having type `Figure` which is not JSON serializable

hi every one,

i want to display a plot in dcc.Graph, but i have an error “returned a value having type Figure
which is not JSON serializable.”

@app.callback(
    Output("products_rating", "figure"),
    Input("third_categories", "value"),
)
def update_stats(third_category, options):
    products = get_products(third_category, category)

    # plot products rating
    bins = [0, 1, 2, 3, 4, 5]
    grouped = products['rating'].value_counts(bins=bins, sort=False).to_frame()
    grouped.reset_index(inplace=True)
    products_rating_figure = px.bar(grouped, x='index', y='rating')

    return products_rating_figure

Hi @mohcin.sarrar

it seams to me that ”index” and “rating” are “strings”.

I don’t understand what are representing then. :thinking:


x='index', y='rating'

this a picture of my dataframe
Screenshot from 2021-08-12 13-05-13

hi every one,

I found the solution, i convert the rating column to numeric, and i add x-axis manualy

products_rating_figure = px.bar(x=['[0,1]', '[1,2]', '[2,3]', '[3,4]', '[4,5]'], y=grouped['rating'].to_list())

thanks a lot @Eduardo

2 Likes