Home > Back-end >  Can you use Dataframe Values as Matplotlib Ylabel?
Can you use Dataframe Values as Matplotlib Ylabel?

Time:10-10

I want to use a value from a specific column in my Pandas dataframe as the Y-axis label. The reason for this is that the label could change depending on the Unit of Measure (UoM) - it could be kg, number of bags etc.

#create function using plant and material input to chart planned and actual manufactured quantities

def filter_df(df, plant: str = "", material: str = ""):
    output_df = df.loc[(df['Plant'] == plant) & (df['Material'].str.contains(material))].reset_index()
    return output_df['Planned_Qty_Cumsum'].plot.area (label = 'Planned Quantity'),\
    output_df['Goods_Receipted_Qty_Cumsum'].plot.line(label = 'Delivered Quantity'),\
    plt.title('Planned and Deliverd Quanties'),\
    plt.legend(),\
    plt.xlabel('Number of Process Orders'),\
    plt.ylabel(output_df['UoM (of GR)']),\
    plt.show()

    
#run function
filter_df(df_yield_data_formatted,'*plant*','*material*')

When running the function I get the following error message:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

CodePudding user response:

Yes you can, but the way you are doing you are saying all the values of the Dataframe in that column and you should indicate what row and column you want for the label, use iloc for instace and it will work.

plt.ylabel(df.iloc[2,1])
  • Related