Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is Matplotlib Y axis showing actual data instead of range

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading