Unable to render bar graph or pie chart on a callback

There are two dropdowns, that show the columns of the dataset and two radio buttons to select the type of chart, bar or pie. If the radio button is selected the chart should render, but unable to render the graph. It shows just the layout with lines, no plot.
‘’’’
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

import pandas as pd
import plotly.graph_objs as go

external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]

app = dash.Dash(name, external_stylesheets=external_stylesheets)

df = pd.read_csv(r’data.csv’)

columns=list(df)

app.layout = html.Div([

html.Div([
    html.Div([
        html.P('X axis'),
        dcc.Dropdown(
            id='xaxis-column',
            options=[{'label': i, 'value': i} for i in columns],
            value=columns[0]
        ),
        html.P('Y axis'),
        dcc.Dropdown(
            id='yaxis-column',
            options=[{'label': i, 'value': i} for i in columns],
            value=columns[1]
        ),
    ],
    style={'width': '48%', 'display': 'inline-block'}),

    html.Div([
        dcc.RadioItems(
            id='chart-type',
            options=[{'label': i, 'value': i} for i in ['Bar plot', 'Pie plot']],
            value='',
            labelStyle={'display': 'inline-block'}
        ),
    ])
]),dcc.Graph(id='graph-output')

])

app.config[‘suppress_callback_exceptions’] = True

@app.callback(Output(‘graph-output’, ‘figure’),
[Input(‘xaxis-column’, ‘value’),
Input(‘yaxis-column’, ‘value’),
Input(‘chart-type’, ‘value’)])
def update_graph(xaxis_column_name, yaxis_column_name, chart_type):

x=df[xaxis_column_name]
y=df[yaxis_column_name]

if chart_type=='Bar plot':

    fig = go.Figure(
        data=[go.Bar(
            x=df[xaxis_column_name], 
            y=df[yaxis_column_name])
              ],
        layout=go.Layout(
            xaxis=dict(
                tickangle=-45,
                tickfont=dict(family='Rockwell', color='crimson', size=14)
            ),
            yaxis=dict(
                showticklabels=True
            ),
        )
    )
    return fig

if chart_type=='Pie plot':
    trace=go.Pie(labels=df[xaxis_column_name], values=df[yaxis_column_name])
    return {'data':[trace]}    

if name == ‘main’:
app.run_server(debug=False)

Not an expert, but try passing an empty go.Figure() in your initial layout for the graph.

I had run into this same issue recently.

Hope it helps!
Scott

Tried passing empty go.Figure, didn’t work. Tried embedding the figure inside a div and then return the div in the callback, worked.:slightly_smiling_face:

Hey if you got it to work that’s what counts. I suspect that’s not an intended behavior, however, as that’s the idiom outlined in a lot of the examples.

This didn’t change the output at all?:

dcc.Graph(id='graph-output', figure=go.Figure())

got the same issue here. can you explain what you did? My graph is not rendering if i change from barchart to piechart

Hey @nickhes, could you show us what you are doing? This thread is quite old, maybe the users aren’t active anymore🙌

Sure, thanks!

first, i declare countinteractions (like this: data000.eval(button_group).nunique()). If this count is >1, i want to output a graph with type ‘bar-chart’. However, if the count=1, i want to show a graph with type ‘pie-chart’. Last thing, which makes it a little more complex is the fact that i added ‘button_group’ as a variable. This is a dropdown that determines my x-axis in the bar-chart or my ‘value’ in the pie-chart.

My setup works perfectly fine, and i can switch from bar-chart to pie-chart. But, if i want to switch back from pie-chart to bar-chart (countinteractions>1), the graph does not seem to render and it kind of glitches (it does not print any errors).

I do not think it makes any sense to add my python code in here, because it is very complex with a lot of variables. Let me know if it helps anyway!

The weirdest part is, if i adjust my screen size, it sometimes does render/load and changes back from the pie-chart to the bar-chart

Thanks a lot in advance. P.S. i have a video of the behaviour, is there a possibility to share?

You could try to reduce your code and create a minimal example, you can find more information concerning this here:

This usually helps a lot and most of the time, the problem can be resolved.