Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot bar graph from 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 

Im 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

like image 749
Alfonso Avatar asked Apr 07 '15 18:04

Alfonso


People also ask

How do we plot a horizontal bar chart using Pandas?

barh() function is used to make a horizontal bar plot. A horizontal bar plot is a plot that presents quantitative data with rectangular bars with lengths proportional to the values that they represent. A bar plot shows comparisons among discrete categories.

Can you plot graphs with Pandas?

Pandas uses the plot() method to create diagrams. We can use Pyplot, a submodule of the Matplotlib library to visualize the diagram on the screen.


1 Answers

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

like image 84
EdChum Avatar answered Oct 21 '22 08:10

EdChum