pythonmatplotlibgraphpython-3.6

Unable to get pie chart to correctly format with matplotlib


my code is this:

 plt.pie(df['Rainfall'].value_counts().values,

        labels = df['Rainfall'].value_counts().index,

        autopct='%1.1f%%')

   plt.show()

  df.groupby('Rainfall').mean()

The issue I am having is that I am trying to show the probability of it raining however its giving many different values. All the values that are above 0 should be a yes that it will rain and all below should say it wont.

I am not sure how to do this and to separate the two

im following this guide: https://www.geeksforgeeks.org/rainfall-prediction-using-machine-learning-python/

The pie chart is attached below

enter image description here

I hope you can help!

Tried to follow the guide and read the documentation but am completely lost


Solution

  • I advice you to use the .apply() method and a lambda function in order to easily classify between positive and negative values.

    For example:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    data = {"location": [1, 1, 4, 3, 2], "Rainfall": [0, 0, 0.1, 0.04, 0.0001]}
    df = pd.DataFrame(data)
    df["Rainfall"] = df["Rainfall"].apply(lambda x: "YES" if x > 0 else "NO")
    
    
    plt.pie(df["Rainfall"].value_counts().values, labels=df["Rainfall"].value_counts().index, autopct="%1.1f%%")
    plt.show()
    

    will output this

    Pie chart