46

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?

2
  • Would it not deliver what you want by just sub-selecting the columns of interest first: ax = df[['V1','V2']].plot(kind='bar', title ="V comp",figsize=(15,10),legend=True, fontsize=12)? Commented Apr 7, 2015 at 18:41
  • @EdChum I was trying along those line but i kept getting error messages. I'm using pandas.pydata.org/pandas-docs/version/0.15.2/visualization.html as reference and could not find an example of what i needed. But you are correct. If you answer it i can accept it. If you can add the explanation of the [ [ ] ] syntax since i was doing ax = df['V1','V2'] Commented Apr 7, 2015 at 18:45

3 Answers 3

84

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, how would you add the relevant error bars to each series?
@CF84 you should post a question rather than ask a question via comments as this doesn't help the SO community
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.