[Python] Boxplot x variables not showing up

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a boxplot of average Na concentrations from different cyclone types (e.g., v0_v1, v1, v0_v2_12, v2_12). However, it seems like the boxplots are being rewritten.

    My code is below:

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

    # Load Excel file
    file_path = 'XRF Co-locations.xlsx' # file name
    sheet = "Masterlist Co-locations" # sheet I want to isolate
    df_dict = pd.read_excel(file_path, sheet_name=[sheet], header=2) # put into dictionary
    df = df_dict[sheet]

    # Clean column names
    df.columns = df.columns.str.strip() # get rid of white spaces

    # Filter based on Notes
    v0_v1_na = df[df["Notes"] == "V0_V1"]["Na (ng/cm2)"]
    v1_na = df[df["Notes_2"] == "V1"]["Na (ng/cm2)_2"]

    v0_v2_12_na = df[df["Notes"] == "V0_V2_12"]["Na (ng/cm2)"]
    v2_12_na = df[df["Notes_2"] == "V2_12"]["Na (ng/cm2)_2"]

    # Combine into DataFrames and compute median for each
    df_mean_v0 = v0_na.median() # calculate the median of v0_na
    df_mean_v1 = v1_na.median() # calculate the median of v1_na

    df_mean_v0_v2_12 = v0_v2_12_na.median() # calculate the median of Na for v0_v2_12
    df_mean_v2_12 = v2_12_na.median() # calculate the median of Na for v2_12

    print(df_mean_v0)
    print(df_mean_v1)

    print(df_mean_v0_v2_12)
    print(df_mean_v2_12)

    # Plot
    plt.figure(figsize=(12, 6))

    # Create boxplots for v0_na and v1_na side by side
    sns.boxplot(data=[v0_na, v1_na, v0_v2_12_na, v2_12_na], orient="v", palette="husl")

    # Adding labels, title, and customization
    plt.ylabel("Na Concentration (ng/cm²)", fontsize=12)
    plt.xlabel("Cyclone Type", fontsize=12)
    plt.xticks([0, 1, 2, 3], ['V0_V1', 'V1', 'V0_V2_12', 'V2_12'], rotation=0) # Custom x-ticks for the two categories
    plt.title("Box Plot of Na (ng/cm²) for V0 and V1 Cyclones", fontsize=14)

    plt.legend()
    plt.show()



    I think the boxplots are being re-written because the boxplot for V0_V1, V1 is different based on if I include V0_V2_12, V2_12 or not (included pictures of the graphs here).

    Does anybody have suggestions on how to fix this?

    Boxplot of V0_V1, V1, V0_V2_12, V2_12 Cyclone

    Boxplot of V0_V1, V1 Cyclone

    Continue reading...

Compartilhe esta Página