Alfonso
Alfonso

Reputation: 481

Plot a bar plot from a Pandas DataFrame

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  45  25
   3  65  38  98  14

I'm trying to create a bar plot to compare columns V1 and V2 by the Hour. When I do:

import matplotlib.pyplot as plt
ax = df.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)

I get a plot and a legend with all the columns' values and names. How can I modify my code so the plot and legend only displays the columns V1 and V2?

Upvotes: 46

Views: 184424

Answers (3)

cottontail
cottontail

Reputation: 23131

Column labels may be passed as axis labels (y-axis admits multiple column labels). Also, since pandas 1.1, axis labels may be passed into the plot() call as well.

df.plot(x='Hour', y=['V1', 'V2'], kind='bar', title="V comp", figsize=(12,6), ylabel='V', rot=0);

result


Another example where bar colors are set and bars are labeled by its height (must have matplotlib>=3.4).

ax = df.plot(x='Hour',                # values on x-axis
             y=['V1', 'V2'],          # values on y-axis
             kind='bar',              # specify that it is a bar-plot
             title="V comp",          # set title
             figsize=(12,6),          # set figure size
             ylabel='V',              # set y-axis label
             rot=0,                   # do not rotate x-ticklabels
             color=['red', 'blue'])   # set bar colors
for heights in ax.containers:
    ax.bar_label(heights)             # label each bar by its height

result2

Upvotes: 0

San Askaruly
San Askaruly

Reputation: 341

A minimal seaborn apporach to plot from pandas dataframe:

import pandas as pd
import seaborn as sns

random_dict = {
    "classes": ["A", "B", "C"],
    "quantities": [100, 300, 200]
}

df = pd.DataFrame.from_dict(random_dict)
sns.barplot(x="classes", y="quantities", data=df)

enter image description here

Upvotes: 1

EdChum
EdChum

Reputation: 394041

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

Upvotes: 84

Related Questions