How to plot multiple data columns in a DataFrame?
Last Updated :
01 Dec, 2023
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() method. To plot multiple data columns in the single frame we simply have to pass the list of columns to the y argument of the plot function.
In this article, we will see how we can plot multiple data columns in a DataFrame.
Plot Columns of Pandas DataFrame
Below are the ways by which we can plot multiple data columns in a Pandas DataFrame in Python:
- Unstacked Multiple Columns of Bar Plots
- Stacked Multiple Columns of Bar Plots
- Multiple Columns of Line Plots
Plot Unstacked Multiple Data Columns of Bar Plots
In this example, a pandas DataFrame is created from a list of city data, and a bar plot is generated using Matplotlib to visualize both the population and the year 2020 for each city. The resulting plot displays unstacked bars with city names on the x-axis and population, and the year on the y-axis.
Python3
import pandas as pd
import matplotlib.pyplot as plt
# Data to be plotted
data = [["New York", 8.6, 20],
["Chicago", 2.7, 20],
["Los Angeles", 3.9, 20],
["Philadelphia", 1.5, 20],
["Houston", 2.1, 20]]
# Form DataFrame from data
df = pd.DataFrame(data, columns=["City", "Population(million)", "Year(2020)"])
# Plot unstacked multiple columns such as population and year from DataFrame
df.plot(x="City", y=["Population(million)", "Year(2020)"],
kind="bar", figsize=(10, 10))
# Display plot
plt.show()
Output:

Stacked Multiple Columns of Bar Plots
In this example, a pandas DataFrame is created from city data, and a stacked bar plot is generated using Matplotlib to visually compare the population and the year 2020 for each city. The 'stacked=True' parameter combines the bars for population and year in a stacked manner.
Python3
import pandas as pd
import matplotlib.pyplot as plt
# Data to be plotted
data = [["New York", 8.6, 20],
["Chicago", 2.7, 20],
["Los Angeles", 3.9, 20],
["Philadelphia", 1.5, 20],
["Houston", 2.1, 20]]
# Form DataFrame from data
df = pd.DataFrame(data, columns=["City", "Population(million)", "Year(2020)"])
# Plot unstacked multiple columns such as population and year from DataFrame
df.plot(x="City", y=["Population(million)", "Year(2020)"],
kind="bar", figsize=(10, 10), stacked=True)
# Display plot
plt.show()
Output:
-660.png)
Plot Multiple Columns of Line Plots in a Pandas DataFrame
In this example, a pandas DataFrame is created from city data, and a line plot is generated using Matplotlib to visualize trends in both population and the year 2020 for each city. The resulting plot displays lines connecting data points for each city along the specified columns. The 'kind="line"' parameter specifies the type of plot, and 'mp.show()' displays the plot.
Python3
import pandas as pd
import matplotlib.pyplot as mp
# data to be plotted
data = [["New York", 8.6, 20],
["Chicago", 2.7, 20],
["Los Angeles", 3.9, 20],
["Philadelphia", 1.5, 20],
["Houston", 2.1, 20]]
# form dataframe from data
df = pd.DataFrame(data, columns=["City", "Population(million)", "Year(2020)"])
# plot multiple columns such as population and year from dataframe
df.plot(x="City", y=["Population(million)", "Year(2020)"],
kind="line", figsize=(10, 10))
# display plot
mp.show()
Output:
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