How to plot a DataFrame in pandas

Pandas is a library used for data analysis and data manipulation in Python. We can use different types of graphs and charts to visualize the results of our analysis in pandas.

Pandas

We can use different plots to visualize the analysis. Some types of plots that can be used are:

  1. Basic plots

  2. Bar plots

  3. Scatter plots

  4. Area plots

Syntax

df.plot(x ='%%Employer%%', y='%%Variable 2 %%', kind = '%% Type of plot %%')

In the syntax, we pass one column of data in the form of Employer as first value to x variable and Average Salary as second value to y variable. Then we'll pass the type of the plot, we want to plot, in the kind variable.

Example

Let's explore how to implement it in the steps below:

Step 1: Create data

The first step is creating data for the analysis. For instance, consider the following table:

Employer

Average Salary

Company A

14000

Company B

15000

Company C

12000

Step 2: Create data frames

In this step, we will pass the value of the column to the dataframes.

import pandas as pd
import matplotlib.pyplot as plt
data = {'Employer': ['Company A','Company B','Company C'],
'Average_Salary': [14000,15000,12000]
}
Defining data frames

Step 3: Defining the plot

Now, we pass the column names to plot the data to the x and y variables and define the type of plot in the kind variable.

import pandas as pd
import matplotlib.pyplot as plt
data = {'Employer': ['Company A','Company B','Company C'],
'Average_Salary': [14000,15000,12000]
}
dataframes = pd.DataFrame(data,columns=['Employer','Average_Salary'])
dataframes.plot(x ='Employer', y='Average_Salary', kind = 'bar')
plt.show()

Explanation

  • Line 4–5: Define data frames to store data.

  • Line 8–9: Pass the data as columns to the x and y variables and pass the plot type as bar to the kind variable.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved