The Wayback Machine - https://web.archive.org/web/20240927140808/https://www.geeksforgeeks.org/how-to-plot-multiple-data-columns-in-a-dataframe/
Open In App

How to plot multiple data columns in a DataFrame?

Last Updated : 01 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

download

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:

download-(1)

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

Pandas - Plot multiple time series DataFrame into a single plot
In this article, we are going to see how to plot multiple time series Dataframe into single plot. If there are multiple time series in a single DataFrame, you can still use the plot() method to plot a line chart of all the time series. To Plot multiple time series into a single plot first of all we have to ensure that indexes of all the DataFrames
4 min read
Plot Multiple Columns of Pandas Dataframe on Bar Chart with Matplotlib
Prerequisites: PandasMatplotlib In this article, we will learn how to plot multiple columns on bar chart using Matplotlib. Bar Plot is used to represent categories of data using rectangular bars. We can plot these bars with overlapping edges or on same axes. Different ways of plotting bar graph in the same chart are using matplotlib and pandas are
2 min read
Pandas Scatter Plot – DataFrame.plot.scatter()
A Scatter plot is a type of data visualization technique that shows the relationship between two numerical variables. For plotting to scatter plot using pandas there is DataFrame class and this class has a member called plot. Calling the scatter() method on the plot member draws a plot between two variables or two columns of pandas DataFrame. Synta
2 min read
Pandas DataFrame assign() Method | Create new Columns in DataFrame
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. The Dataframe.assign() method assigns new columns to a DataFrame, returning a new object (a copy) with the new columns added to the original one
4 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 have to call the show() function to display the plot
2 min read
Add multiple columns to dataframe in Pandas
In Pandas, we have the freedom to add columns in the data frame whenever needed. There are multiple ways to add columns to pandas dataframe. Add multiple columns to a DataFrame using Lists C/C++ Code # importing pandas library import pandas as pd # creating and initializing a nested list students = [['jackma', 34, 'Sydeny', 'Australia'], ['Ritika',
3 min read
Split single column into multiple columns in PySpark DataFrame
pyspark.sql.functions provide a function split() which is used to split DataFrame string Column into multiple columns. Syntax: pyspark.sql.functions.split(str, pattern, limit=- 1) Parameters: str: str is a Column or str to split.pattern: It is a str parameter, a string that represents a regular expression. This should be a Java regular expression.l
4 min read
How to select and order multiple columns in Pyspark DataFrame ?
In this article, we will discuss how to select and order multiple columns from a dataframe using pyspark in Python. For this, we are using sort() and orderBy() functions along with select() function. Methods UsedSelect(): This method is used to select the part of dataframe columns and return a copy of that newly selected dataframe. Syntax: datafram
2 min read
How to Order PysPark DataFrame by Multiple Columns ?
In this article, we are going to order the multiple columns by using orderBy() functions in pyspark dataframe. Ordering the rows means arranging the rows in ascending or descending order, so we are going to create the dataframe using nested list and get the distinct data. orderBy() function that sorts one or more columns. By default, it orders by a
2 min read
How to rename multiple columns in PySpark dataframe ?
In this article, we are going to see how to rename multiple columns in PySpark Dataframe. Before starting let's create a dataframe using pyspark: C/C++ Code # importing module import pyspark from pyspark.sql.functions import col # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and giving
2 min read
Drop One or Multiple Columns From PySpark DataFrame
In this article, we will discuss how to drop columns in the Pyspark dataframe. In pyspark the drop() function can be used to remove values/columns from the dataframe. Syntax: dataframe_name.na.drop(how=”any/all”,thresh=threshold_value,subset=[“column_name_1″,”column_name_2”]) how – This takes either of the two values ‘any’ or ‘all’. ‘any’, drop a r
3 min read
PySpark - Sort dataframe by multiple columns
In this article, we are going to see how to sort the PySpark dataframe by multiple columns. It can be done in these ways: Using sort()Using orderBy() Creating Dataframe for demonstration: C/C++ Code # importing module import pyspark # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and giv
2 min read
How to Rename Multiple PySpark DataFrame Columns
In this article, we will discuss how to rename the multiple columns in PySpark Dataframe. For this we will use withColumnRenamed() and toDF() functions. Creating Dataframe for demonstration: C/C++ Code # importing module import pyspark # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and
2 min read
Python PySpark - DataFrame filter on multiple columns
In this article, we are going to filter the dataframe on multiple columns by using filter() and where() function in Pyspark in Python. Creating Dataframe for demonestration: C/C++ Code # importing module import pyspark # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and giving an app nam
2 min read
Split dataframe in Pandas based on values in multiple columns
In this article, we are going to see how to divide a dataframe by various methods and based on various parameters using Python. To divide a dataframe into two or more separate dataframes based on the values present in the column we first create a data frame. Creating a DataFrame for demonestration C/C++ Code # importing pandas as pd import pandas a
3 min read
How to select multiple columns in a pandas dataframe
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. In this article, we will discuss all the different ways of selecting multiple columns in a Pandas DataFrame. Select Multiple Columns in
5 min read
How to sort a Pandas DataFrame by multiple columns in Python?
Sorting is a fundamental operation applied to dataframes to arrange data based on specific conditions. Dataframes can be sorted alphabetically or numerically, providing flexibility in organizing information. This article explores the process of sorting a Pandas Dataframe by multiple columns, demonstrating the versatile capabilities of Pandas in han
6 min read
Unnest (Explode) Multiple List Columns In A Pandas Dataframe
An open-source manipulation tool that is used for handling data is known as Pandas. Have you ever encountered a dataset that has columns with data as a list? In such cases, there is a necessity to split that column into various columns, as Pandas cannot handle such data. In this article, we will discuss the same, i.e., unnest or explode multiple li
6 min read
How to drop one or multiple columns in Pandas Dataframe
Let’s discuss how to drop one or multiple columns in Pandas Dataframe. To Delete a column from a Pandas DataFrame or Drop one or multiple columns in a Pandas Dataframe can be achieved in multiple ways. # Drop column 'B'df = df.drop('B', axis=1)# Drop columns multiple column df = df.drop(['B', 'C'], axis=1)There are various methods to drop one or mu
10 min read
Plot 2D data on 3D plot in Python
In this article, we will be learning about how to plot 2D data on 3D plot in Python. We will be demonstrating two methods in order to learn the concept. The first method will be using Matplotlib.pyplot.gca() function which is a pyplot module of the matplotlib library. But before that, we need to configure where are we going to create our project an
4 min read
Change Data Type for one or more columns in Pandas Dataframe
Let's see How To Change Column Type in Pandas DataFrames, There are different ways of changing DataType for one or more columns in Pandas Dataframe. Change column type into string object using DataFrame.astype() DataFrame.astype() method is used to cast pandas object to a specified dtype. This function also provides the capability to convert any su
3 min read
Select Columns with Specific Data Types in Pandas Dataframe
In this article, we will see how to select columns with specific data types from a dataframe. This operation can be performed using the DataFrame.select_dtypes() method in pandas module. Syntax: DataFrame.select_dtypes(include=None, exclude=None)Parameters : include, exclude : A selection of dtypes or strings to be included/excluded. At least one o
2 min read
PyQtGraph - Getting Plot Item from Plot Window
In this article we will see how we can get the plot item of plot window in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) and s
2 min read
Time Series Plot or Line plot with Pandas
Prerequisite: Create a Pandas DataFrame from Lists Pandas is an open-source library used for data manipulation and analysis in Python. It is a fast and powerful tool that offers data structures and operations to manipulate numerical tables and time series. Examples of these data manipulation operations include merging, reshaping, selecting, data cl
6 min read
How to Plot a Vertical Line on a Time Series Plot in Pandas
When working with time series data in Pandas, it is often necessary to highlight specific points or events in the data. One effective way to do this is by plotting vertical lines on the time series plot. In this article, we will explore how to plot a vertical line on a time series plot in Pandas, covering the necessary steps, functions, and paramet
3 min read
Python | Pandas Dataframe.plot.bar
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas DataFrame.plot.bar() plots the graph vertically in form of rectangular bars. Syntax : DataFrame.plot.bar(x=None, y=None, **kwds)
2 min read
How to plot a Pandas Dataframe with Matplotlib?
Prerequisites: PandasMatplotlib Data visualization is the most important part of any analysis. Matplotlib is an amazing python library which can be used to plot pandas dataframe. There are various ways in which a plot can be generated depending upon the requirement. Comparison between categorical data Bar Plot is one such example. To plot a bar gra
3 min read
How to plot a dataframe using Pandas?
Pandas is one of the most popular Python packages used in data science. Pandas offer a powerful, and flexible data structure ( Dataframe & Series ) to manipulate, and analyze the data. Visualization is the best way to interpret the data.  Python has many popular plotting libraries that make visualization easy. Some of them are Matplotlib, Seabo
8 min read
Select columns in PySpark dataframe
In this article, we will learn how to select columns in PySpark dataframe. Function used: In PySpark we can select columns using the select() function. The select() function allows us to select single or multiple columns in different formats. Syntax: dataframe_name.select( columns_names ) Note: We are specifying our path to spark directory using th
4 min read
Difference of two columns in Pandas dataframe
Difference of two columns in pandas dataframe in Python is carried out by using following methods : Method #1 : Using ” -” operator. import pandas as pd # Create a DataFrame df1 = { 'Name':['George','Andrea','micheal', 'maggie','Ravi','Xien','Jalpa'], 'score1':[62,47,55,74,32,77,86], 'score2':[45,78,44,89,66,49,72]} df1 = pd.DataFrame(df1,columns=
2 min read