So I'm trying to create a simple panel with 3 graphs, a dropdown menu, and a description for each. However, only one of the graphs is interactive along with its description. The main issue is that the interactive graph is not updating, instead a new graph is being added to the panel thus making the panel longer and longer. The code I have until now is as follows:
type hereimport matplotlib.pyplot as plt
import ipywidgets as widgets
from IPython.display import display
import panel as pn
import pandas as pd
# Assume data is your DataFrame containing the required data
# Define graph 1 (no interactivity)
def plot_graph_1(data):
target_counts = data['Meets end target by completion of project'].value_counts()
plt.figure(figsize=(8, 6))
target_counts.plot(kind='bar', color=['green', 'red'])
plt.title('Projects Meeting End Target by Completion of Project')
plt.xlabel('Meets End Target')
plt.ylabel('Number of Projects')
plt.xticks(rotation=0)
plt.show()
# Define graph 2 (interactive)
def plot_graph_2(data, selected_sdg):
sdg_data = data[data['SDG Goal'] == selected_sdg]
sdg_counts = sdg_data['Meets end target by completion of project'].value_counts()
yes_color = 'green'
no_color = 'red'
plt.figure(figsize=(6, 4))
sdg_counts[sdg_counts.index.isin(['Yes', 'No'])].reindex(['Yes', 'No']).plot(kind='bar', color=[yes_color, no_color])
plt.title(f'Meeting End Target by Completion Date for {selected_sdg}')
plt.xlabel('Meets End Target')
plt.ylabel('Number of Projects')
plt.xticks(rotation=0)
plt.show()
# Define dropdown menu for SDG selection
sdg_dropdown = widgets.Dropdown(options=data['SDG Goal'].unique(), description='Select SDG Goal:', disabled=False)
# Define dropdown (interactive)
def dropdown_interactive(data, selected_sdg):
sdg_data = data[(data['SDG Goal'] == selected_sdg) & (data['Meets end target by completion of project'] == 'No')]
avg_time = sdg_data['Years until End target is met'].mean()
print(f"The average time after completion date for projects to meet their End target for SDG {selected_sdg} is: {avg_time:.2f} years")
# Define graph (not interactive)
def plot_graph(data):
avg_project_length_by_sdg = data.groupby('SDG Goal')['Total Project Length (Years)'].mean().sort_values()
colors = plt.cm.tab10.colors[:len(avg_project_length_by_sdg)]
plt.figure(figsize=(10, 6))
avg_project_length_by_sdg.plot(kind='bar', color=colors)
plt.title('Average Time to reach SDG Goal')
plt.xlabel('SDG Goal')
plt.ylabel('Average Total Project Length (Years)')
plt.xticks(rotation=45)
plt.show()
# Define dropdown (interactive)
def dropdown_interactive2(data, selected_sdg):
sdg_data = data[data['SDG Goal'] == selected_sdg]
avg_project_length = sdg_data['Total Project Length (Years)'].mean()
print(f"The average total time for SDG {selected_sdg} to be met is: {avg_project_length:.2f} years")
# Display the dropdown menu and observe changes
display(sdg_dropdown)
# Create Panel objects for each component
plot1_panel = pn.bind(plot_graph_1, data)
plot2_panel = pn.bind(plot_graph_2, data, sdg_dropdown)
dropdown_interactive_panel = pn.bind(dropdown_interactive, data, sdg_dropdown)
plot_graph_panel = pn.bind(plot_graph, data)
dropdown_interactive2_panel = pn.bind(dropdown_interactive2, data, sdg_dropdown)
# Define the layout for the dashboard
top_row = pn.Row(
plot2_panel,
dropdown_interactive_panel
)
bottom_row = pn.Row(
plot1_panel,
plot_graph_panel
)
# Combine all components into a single dashboard layout
dashboard = pn.Column(
'# Dashboard Title',
top_row,
bottom_row
)
# Show the dashboard
dashboard.show()
Only graph 2 along with the defined dropdown should be updating, however they are being added once again. If any more context such as pictures or a quick video to showcase what is happening is needed, please do let me know. Thank you.
I tried by doing it part by part, specifically graph 2 along with the specific descriptions and the dropdown menu, and that way it worked. However, when I started adding the static graphs everything started breaking down.