Python script to build Figure 4 (bottom graph)

"script-number-123": Python script for Figure 4 (bottom graph).
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.lines as mlines

def create_exact_bbb_chart_hardcoded():
    # 1. Data (X-axis and Y-axes)
    x_data = [0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0]
    
    y_drugbank = [0.24, 5.14, 3.56, 3.68, 2.91, 2.55, 3.2, 2.47, 2.35, 2.39, 2.63, 2.51, 2.91, 3.11, 2.95, 4.41, 5.18, 5.99, 7.81, 20.31, 13.71]
    y_bbb_neg = [1.02, 18.95, 14.11, 8.07, 6.07, 2.91, 3.19, 2.07, 2.18, 1.79, 1.97, 2.25, 1.72, 2.67, 2.42, 3.05, 3.02, 4.39, 5.3, 8.85, 4.0]
    y_bbb_pos = [0.04, 0.36, 0.38, 0.44, 0.28, 0.38, 0.52, 0.4, 0.48, 0.73, 0.44, 0.91, 0.99, 0.93, 1.25, 2.14, 3.23, 4.34, 9.57, 38.53, 33.63]
    y_molport_nonchiral = [0.0, 0.1, 0.24, 0.39, 0.52, 0.68, 0.88, 1.09, 1.39, 1.76, 2.19, 2.7, 3.44, 4.36, 5.55, 7.17, 9.4, 12.28, 16.75, 22.62, 6.48]
    y_molport_chiral = [0.0, 0.24, 0.43, 0.74, 1.01, 1.29, 1.56, 1.87, 2.27, 2.71, 3.11, 3.32, 3.59, 4.14, 4.95, 6.11, 7.95, 10.77, 15.03, 21.92, 6.97]

    # Exact legend names and colors assignment
    comparison_data = [
        {"name": "DrugBank", "y": y_drugbank, "color": "#C00000", "marker": "^"}, 
        {"name": "BBB+", "y": y_bbb_pos, "color": "#E97132", "marker": "s"},
        {"name": "BBB-", "y": y_bbb_neg, "color": "#00B050", "marker": "o"},
        {"name": "MolPort nonchiral carbon", "y": y_molport_nonchiral, "color": "#0F9ED5", "marker": "v"},
        {"name": "MolPort chiral carbon", "y": y_molport_chiral, "color": "#A02B93", "marker": "D"} 
    ]

    # 2. Chart Configuration
    threshold_bounds = [0.0, 0.3, 0.8, 1.0]
    threshold_colors = ["#C14E4E", "#FFDF33", "#63C28D"] 
    threshold_labels = ["Low", "Moderate", "High"]

    # Figure dimensions configuration
    fig_width = 11 / 2.54 
    fig_height = 12 / 2.54 
    bg_color = 'white'

    fig, ax = plt.subplots(figsize=(fig_width, fig_height))
    fig.patch.set_facecolor(bg_color)
    
    # Dynamically adjust the upper limit of the Y-axis
    max_y = max([np.max(d["y"]) for d in comparison_data]) * 1.15
    ax.set_xlim(0.0, 1.0)
    ax.set_ylim(0, max_y)

    # Draw background color blocks (using alpha=0.75 so colors stand out without obscuring the lines)
    for i in range(len(threshold_colors)):
        start = threshold_bounds[i]
        width = threshold_bounds[i+1] - threshold_bounds[i]
        rect = patches.Rectangle((start, 0), width, max_y, facecolor=threshold_colors[i], alpha=0.75, zorder=0)
        ax.add_patch(rect)

    # Plot the lines
    for dataset in comparison_data:
        ax.plot(x_data, dataset["y"], color=dataset["color"], marker=dataset["marker"], 
                markersize=5, linestyle='-', linewidth=1.5, label=dataset["name"],
                markerfacecolor=dataset["color"], markeredgecolor='white', zorder=10)

    # Axes formatting
    ax.set_xticks(np.arange(0, 1.1, 0.1))
    ax.set_xlabel("Probability of crossing the BBB", fontsize=9, weight='bold')
    ax.set_ylabel("% Frequency", fontsize=9, weight='bold')
    
    # Remove top and right spines
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)

    # Add the top text labels (Low, Moderate, High)
    for i, label in enumerate(threshold_labels):
        mid_point = (threshold_bounds[i] + threshold_bounds[i+1]) / 2
        ax.text(mid_point, max_y * 0.96, label, fontsize=8, ha='center', weight='bold', color='black')

    # LEGEND (Centered in the "Moderate" zone, single column to fit perfectly)
    ax.legend(loc='upper center', bbox_to_anchor=(0.55, 0.92), 
              fontsize=8, frameon=False, ncol=1)

    # Adjust margins
    plt.subplots_adjust(bottom=0.30, top=0.92, left=0.12, right=0.95)
    
    # Save the plot
    output_name = "Figure_4_bottom-graph.png"
    plt.savefig(output_name, dpi=300, bbox_inches='tight')
    plt.close()
    print(f"Success! Chart generated and saved as: {output_name}")

if __name__ == "__main__":
    create_exact_bbb_chart_hardcoded()