In this tutorial, we will look at how to plot a pandas series values on a line plot.
Line Plot from a Pandas Series
To plot a pandas series, you can use the pandas series
plot() function which, by default, plots the series values on a line plot. The following is the syntax:
# using pandas series plot() s.plot()
Here, s is the pandas series you want to plot. The pandas series plot() function returns a matplotlib axes object to which you can add additional formatting.
Note that you can use the pandas series
plot() function to plot other plots like histograms, bar charts, pie charts, etc. For this, pass the appropriate value to the
kind parameter which is ‘line’ by default.
Let’s look at some examples of plotting a pandas series values. First, we’ll create a sample pandas series which we will be using throughout this tutorial.
import pandas as pd # pandas series of Employee Count from 2015 to 2020 employee_counts = pd.Series(index=[2015, 2016, 2017, 2018, 2019, 2020], data=[7, 11, 25, 102, 150, 214], name='Employees') # display the series print(employee_counts)
2015 7 2016 11 2017 25 2018 102 2019 150 2020 214 Name: Employees, dtype: int64
You can see the contents of the series object above. We now have a pandas series containing the number of employees at a startup, let’s call it “A” from 2015 to 2020 with the year as its index.
1. Plot Series using plot()
Let’s see the usage of the pandas series
plot() function to display a line chart of the series values.
You can see that by default the pandas series
plot() function creates a line plot of the series values. Note that in this case, the chart shows a clear increase in the employee count.
For more on the pandas series plot() function, refer to its documentation.
2. Customize plot formatting
You can also customize the formatting of the chart. For instance, you can add the axes labels, chart title, change colors and fonts, etc. Since the returned plot is a matplotlib axes object, you can apply any formatting that would work with matplotlib charts.
Let’s go ahead and axes labels and title to our plot.
# create the plot ax = employee_counts.plot() # set the x-axis label ax.set_xlabel("Year") # set the y-axis label ax.set_ylabel("Count") # set the title ax.set_title("Employee Growth at A")
You can see the above chart has “Year” as its x-axis label, “Count” as its y-axis label, and “Employee Growth at A” as its title.
For more on line charts and their formatting refer to our tutorial on matplotlib line charts.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on pandas series –
- Convert Pandas Series to a DataFrame
- Convert Pandas Series to a List
- Convert Pandas Series to a NumPy Array
- Convert Pandas Series to a Dictionary
- Sort a Pandas Series
- Append Two Pandas Series
- Apply a Function to a Pandas Series
- Pandas – Shift column values up or down
- Plot a Histogram of Pandas Series Values
- Create a Pie Chart of Pandas Series Values
- Plot a Bar Chart of Pandas Series Values
- Create a Boxplot from Pandas Series Values
- Create a Density Plot from Pandas Series Values