Passen Sie die Balken im Diagramm basierend auf der Teilmengenbedingung anPython

Python-Programme
Guest
 Passen Sie die Balken im Diagramm basierend auf der Teilmengenbedingung an

Post by Guest »

Ich stehe vor einem Problem, wie im Bild unten gezeigt. Ich versuche, das Balkendiagramm basierend auf den Behandlungen als Gruppe und basierend auf dem Dropdown-Menü zu erstellen. Wenn ich die Auswahl einer Behandlung aufhebe, sollte der Balken verschwinden und die Balken davon anzeigen Verbleibende Behandlungen. Wenn ich jedoch eine Behandlung abwähle, entstehen Lücken. Wie Sie sie vermeiden können, helfen Sie bitte
Image

Code: Select all

def plot_(data,selected_trt):
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import streamlit as st

# Create the bar chart
plt.figure(figsize=(10, 7))
# plt.barh(data['USUBJID_numeric'], data['max_ady'], color='skyblue', edgecolor='black', alpha=0.8)

data = data[data['TRT01P'].isin(selected_trt)]

# Example dataset (replace with your data's column of treatment groups)
unique_treatments = data['TRT01P'].unique()

# Define a set of colors (expand or change as needed)
color_palette = plt.cm.Pastel1.colors  # Use a colormap (e.g., Pastel1, Set2, etc.)
num_colors = len(color_palette)

# Generate the treatment_colors dictionary dynamically
treatment_colors = {
treatment: color_palette[i % num_colors]  # Cycle through the color palette if treatments exceed colors
for i, treatment in enumerate(unique_treatments)
}

# Assign colors based on the treatment group
data['color'] = data['TRT01P'].map(treatment_colors)
xupper = data['max_ady'].max()

# Create the bar chart
plt.figure(figsize=(10, 7))

# for _, row in data.iterrows():
for _, row in data.iterrows():
plt.barh(
row['USUBJID_numeric'],  # Subject on the y-axis
row['max_ady'],          # Days of treatment on the x-axis
color=row['color'],      # Color based on treatment group
edgecolor='black',
alpha=0.8
)

legend_elements = [
Line2D([0], [0], color=color, lw=4, label=treatment)
for treatment, color in treatment_colors.items()
]
plt.legend(handles=legend_elements, title='Treatments', loc='best')
# Update the y-axis ticks to show original USUBJID values
plt.yticks(data['USUBJID_numeric'], data['USUBJID'])

# Add labels and title
plt.xlabel('Days of Treatment')
plt.ylabel('Subjects')
# plt.title('Swimmer Plot for Treatment Exposure')
# Adjust axis limits to start at (0, 0)
plt.margins(x=0, y=0.01)
ax = plt.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.xlim(0, xupper+100)

# Display the plot in Streamlit
st.pyplot(plt.gcf())

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post