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.
We can use different plots to visualize the analysis. Some types of plots that can be used are:
Basic plots
Bar plots
Scatter plots
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 pdimport matplotlib.pyplot as pltdata = {'Employer': ['Company A','Company B','Company C'],'Average_Salary': [14000,15000,12000]}
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 pdimport matplotlib.pyplot as pltdata = {'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
xandyvariables and pass the plot type as bar to thekindvariable.
Free Resources