Plot Multiple Columns of Pandas Dataframe on Bar Chart with Matplotlib
Last Updated :
19 May, 2025
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
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
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
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
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.
Related articles
Similar Reads
How to display bar charts in Pandas dataframe on specified columns? In this article we will see how to display bar charts in dataframe on specified columns. For doing this task we are using DataFrame.style.bar() method of Pandas Dataframe. Syntax: pandas.DataFrame.style.bar(columns_list, color) Return: Dataframe with the given color bar strips on the positive defini
1 min read
How to plot a Pandas Dataframe with Matplotlib? We have a Pandas DataFrame and now we want to visualize it using Matplotlib for data visualization to understand trends, patterns and relationships in the data. In this article we will explore different ways to plot a Pandas DataFrame using Matplotlib's various charts. Before we start, ensure you ha
2 min read
How to plot multiple data columns in a DataFrame? Python comes with a lot of useful packages such as pandas, matplotlib, numpy, etc. To use DataFrame, we need a Pandas library and to plot columns of a DataFrame, we require matplotlib. Pandas has a tight integration with Matplotlib. You can plot data directly from your DataFrame using the plot() met
3 min read
How to Show All Columns of a Pandas DataFrame? Pandas limit the display of rows and columns, making it difficult to view the full data, so let's learn how to show all the columns of Pandas DataFrame. Using pd.set_option to Show All Pandas ColumnsPandas provides a set_option() function that allows you to configure various display options, includi
2 min read
How to Plot Multiple Series from a Pandas DataFrame? In this article, we will discuss how to plot multiple series from a dataframe in pandas. Series is the range of the data  that include integer points we cab plot in pandas dataframe by using plot() function Syntax: matplotlib.pyplot(dataframe['column_name']) We can place n number of series and we ha
2 min read