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
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read