Open In App

Plot Multiple Columns of Pandas Dataframe on Bar Chart with Matplotlib

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Plotting multiple columns of a pandas DataFrame on a bar chart with Matplotlib helps compare data across categories. By using a categorical column on the x-axis and numeric columns as values, you can show grouped bars side by side. For example, you can compare age and height for each name in a DataFrame. Here are some simple and efficient ways to do this.

Using df.plot(kind="bar")

This is the simplest method when working with pandas DataFrames. You can easily plot multiple columns as grouped bars by specifying the x and y values. It’s quick and convenient, especially for small to medium datasets

Python
import pandas as pd, matplotlib.pyplot as plt

df = pd.DataFrame({'Name': ['John', 'Sammy', 'Joe'], 'Age': [45, 38, 90], 'Height(in cm)': [150, 180, 160]})

df.plot(x="Name", y=["Age", "Height(in cm)"], kind="bar")
plt.ylabel("Values"); 
plt.show()

Output

Output
Using df.plot(kind="bar")

Explanation: x="Name" parameter sets the names as labels on the x-axis, while the y=["Age", "Height(in cm)"] parameter plots both age and height values for each person side by side. To add clarity, plt.ylabel("Values") labels the y-axis and plt.show() displays the chart.

Using plt.bar

This method gives you full control over bar positions and layout by manually specifying coordinates using NumPy. It's more verbose but highly customizable, making it suitable when you need fine-tuned visuals or want to plot multiple bar groups with precise alignment.

Python
import numpy as np, matplotlib.pyplot as plt

names = ['John', 'Sammy', 'Joe']; ages = [45, 38, 90]; heights = [150, 180, 160]
x = np.arange(len(names)); w = 0.35

plt.bar(x - w/2, ages, w, label='Age')
plt.bar(x + w/2, heights, w, label='Height (cm)')
plt.xticks(x, names)
plt.legend()
plt.show()

Output

Output
Using plt.bar()

Explanation: np.arange(len(names)) creates positions for the bars, and w sets their width. Two plt.bar() calls plot ages and heights side by side by shifting their positions left and right. plt.xticks() sets the names as x-axis labels, plt.legend() adds a legend and plt.show() displays the chart.

Using df.set_index().plot().bar()

A more elegant and structured way using pandas, this approach first sets an index and then plots. It keeps the code clean and avoids repetition, especially useful when dealing with indexed data.

Python
df.set_index('Name')[['Age', 'Height(in cm)']].plot.bar()
plt.ylabel("Values")
plt.show()

Output

Output
Using df.set_index().plot().bar()

Explanation: df.set_index('Name') sets the 'Name' column as the index, allowing [['Age', 'Height(in cm)']] to select these columns for plotting. The plot.bar() method then creates a grouped bar chart using the index as x-axis labels. plt.ylabel("Values") adds a y-axis label and plt.show() displays the chart.

Using melt()

Best for more complex and polished visualizations, this method reshapes the data into a long format using melt, which is then ideal for Seaborn’s grouped bar plots. It’s especially useful when dealing with multiple categories

Python
import seaborn as sns

df = df.melt(id_vars='Name', value_vars=['Age', 'Height(in cm)'], var_name='Attribute', value_name='Value')
sns.barplot(data=df, x='Name', y='Value', hue='Attribute')
plt.show()

Output

Output
Using melt()

Explanation: melt() reshapes the DataFrame to long format with 'Name' as ID and 'Age' and 'Height' as attributes. sns.barplot() creates a grouped bar chart with names on the x-axis and colors bars by attribute. plt.show() displays the plot.


Next Article

Similar Reads