Home > Back-end >  Why is Matplotlib Y axis showing actual data instead of range
Why is Matplotlib Y axis showing actual data instead of range

Time:03-07

I'm having a problem with something I've done many times before and I'm just not seeing what is going wrong. I have a dataframe with two column - Date and Results. I want to plot the Daate along the X axis and the results along the Y axis. The dataframe is in date order. I have tried with date as the index, and just sorted. But instead of the Y axis being a range from low to high, it is showing the actual result instead. Here is an example: enter image description here

The x-axis is jumping around instead of the bars. The code couldn't be any simpler:

def results_chart(df):

    fig, ax = plt.subplots()

    ax.set_xlabel('Date', fontsize=10)
    ax.set_ylabel('Result', fontsize=12)
    ax.set_title("Results")

    ax.bar(df["Date"], df["Result"])
    plt.tight_layout()
    graph = get_graph()

    return graph

I must be missing something very basic. Help appreciated,

CodePudding user response:

It seems that the values in df['Result'] are strings. You will need to convert them to a float.

df['Result'] = df['Result'].astype(float)
  • Related