I am writing some code to have a plot containing two graphs, a bar chart and a table. Both are on the same dataframe (an example one has been recreated for this post), so I have set up two buttons one for each graph. However, when I click the button that should hide the table it doesn’t disappear. Code is linked below with an example, thanks for any help.
import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame({'Animals': ['dog', 'cat', 'sheep', 'pig'], 'Count': [4, 7, 2, 8]},
columns=['Animals', 'Count'])
# Create example dataframe
layout = go.Layout(
title="EXAMPLE",
title_font=dict(size=24, color='#707070'),
font=dict(size=16, color='#535353'),
xaxis=dict(
title='Count',
gridcolor='rgba(0,0,0,0)'
),
yaxis=dict(
title='Animals',
gridcolor='rgba(0,0,0,0)',
showticklabels=False
),
width=900,
height=600,
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
)
# Create layout
fig = go.Figure(layout=layout)
# Create empty figure
fig.add_trace(go.Bar(x=list(df['Count']), y=list(df['Animals']), orientation="h",
text=list(df['Animals']), textposition="inside",
marker=dict(line=dict(width=5)), visible=False))
# Add bar graph
fig.add_trace(go.Table(header=dict(values=['Animals', 'Count'], line_color='black',
fill_color='rgba(0,0,0,0)', font=dict(size=14, color='#535353')),
cells=dict(values=[list(df['Animals'][::-1]), list(df['Count'][::-1])],
line_color='black', fill_color='rgba(0,0,0,0)'), visible=False))
# Add table
# Make 0th trace (bar graph) visible
fig.data[0].visible = True
fig.update_layout(
updatemenus=[
dict(
type="buttons",
direction="left",
buttons=list([
dict(label='Graph',
method='update',
args=[{'visible': [True, False]}]),
dict(label='Table',
method='update',
args=[{'visible': [False, True]}])
]),
pad={"r": 10, "t": 10},
showactive=True,
x=0.11,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
# Add buttons
fig.show()
This happens after selecting the Table graph, then selecting the Bar chart graph button