[Python] Heatmap (seaborn) doesn't show all Ylabels [duplicate]

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    My heatmap doesn't want to show all Ylabels.

    This is the plot [​IMG]

    And this is the code

    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt

    # Load the dataset from a file (e.g., 'data.csv')
    file_path = 'data.csv' # Replace with your file path
    df = pd.read_csv(file_path, sep='\t')

    # Convert the "VAF from Genotyping" column to numeric after removing the percentage sign
    df['VAF from Genotyping'] = df['VAF from Genotyping'].str.rstrip('%').astype(float)

    # Sort the dataframe by "VAF from Genotyping" in descending order
    df_sorted = df.sort_values(by='VAF from Genotyping', ascending=False)

    # Modify Sample-Run labels to include #Reads in BAM and VAF from Genotyping
    df_sorted["Sample-Run"] = df_sorted["Sample-Run"] + " (" + df_sorted["#Reads in BAM"].astype(str) + " Reads, " + df_sorted["VAF from Genotyping"].astype(str) + "% VAF)"

    # Convert detection status to binary
    threshold_cols = ["6K", "10K", "15K", "20K", "25K", "35K"]
    df_binary = df_sorted[threshold_cols].replace({"No Mutation Detected": 0, "Variant": 1})

    # Ensure binary index matches Sample-Run
    df_binary.index = df_sorted["Sample-Run"]


    print(len(df_binary))

    # --- Heatmap Plot ---
    plt.figure(figsize=(12, 8))
    sns.heatmap(df_binary, cmap="Blues", annot=True, cbar=True, fmt="d")
    plt.title("Mutation Detection Matrix Across Thresholds", fontsize=14)
    plt.xlabel("Read Thresholds", fontsize=12)
    plt.ylabel("Wnumber-RunID Read VAF", fontsize=12)

    plt.gca().set_yticklabels(plt.gca().get_yticklabels(), fontsize=10)


    # Save heatmap
    heatmap_file = "detection_matrix_with_metadata.png"
    plt.tight_layout()
    plt.savefig(heatmap_file)
    plt.close() # Close the figure

    # Save the sorted dataset to a new file
    output_file = 'sorted_data_with_metadata.csv'
    df_sorted.to_csv(output_file, index=False, sep='\t')

    # Confirmation
    print(f"Sorted data saved to '{output_file}'")
    print(f"Heatmap saved as '{heatmap_file}'")


    The print(len(df_binary)) showed me that the heatmap function takes 54 elements and I can see that in the number of 0s in the first column of the heatmap. However, the plot only show around 27 Ylabels. Why???

    Continue reading...

Compartilhe esta Página