[Python] How do I set the x-axis names?

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    Hope someone can help.
    I'm trying to do a heat map and my x-axis is showing numbers instead of the weeks.

    How can I do this ?
    I'm using Python in Excel. I understand Python is easier outside of it, but we need this to be in Excel.

    My data is like this.

    Names (W42) Week1 (X42) Week2 (Y42) ...
    Name1 (W43) data1 data2 ...
    Name2 (W44) data3 data4 ...

    Issue:
    My axis is showing numbers instead of the weeks.
    x-axis image

    Code:

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

    # Load the data from Excel (including wine names and week columns)
    df = xl("W142:CH166")

    # Set the first column as the index (wine names)
    df.set_index(df.columns[0], inplace=True)

    # Convert all data to numeric
    df = df.apply(pd.to_numeric, errors='coerce')

    # Drop rows where all values are NaN or zero
    df = df.dropna(how='all')
    df = df[(df != 0).any(axis=1)]

    # Ensure column names are strings for plotting
    df.columns = df.columns.astype(str)

    # Plot
    plt.figure(figsize=(50, 5), dpi=100)
    sns.heatmap(df, cmap='Blues', annot=False, linewidths=0.5, linecolor='white')

    # Explicitly set x-axis labels to ensure they show up
    plt.xticks(ticks=range(len(df.columns)), labels=df.columns, rotation=45)
    plt.yticks(rotation=0)
    plt.title('Heat Map of Wine Sales Over Time')
    plt.show()


    I expected the x-axis to show the Week1, Week2, etc

    Continue reading...

Compartilhe esta Página