Plot bar graph from Pandas DataFrame

后端 未结 1 490
执笔经年
执笔经年 2020-12-02 13:01

Assuming i have a DataFrame that looks like this:

Hour | V1 | V2 | A1 | A2
 0   | 15 | 13 | 25 | 37  
 1   | 26 | 52 | 21 | 45 
 2   | 18 | 45 |         


        
相关标签:
1条回答
  • 2020-12-02 14:00

    To plot just a selection of your columns you can select the columns of interest by passing a list to the subscript operator:

    ax = df[['V1','V2']].plot(kind='bar', title ="V comp", figsize=(15, 10), legend=True, fontsize=12)
    

    What you tried was df['V1','V2'] this will raise a KeyError as correctly no column exists with that label, although it looks funny at first you have to consider that your are passing a list hence the double square brackets [[]].

    import matplotlib.pyplot as plt
    ax = df[['V1','V2']].plot(kind='bar', title ="V comp", figsize=(15, 10), legend=True, fontsize=12)
    ax.set_xlabel("Hour", fontsize=12)
    ax.set_ylabel("V", fontsize=12)
    plt.show()
    

    enter image description here

    0 讨论(0)
提交回复
热议问题