Line plot plotted from a pandas series

Plot Pandas Series as a Line Plot

In this tutorial, we will look at how to plot a pandas series values on a line plot.

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)

Output:

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.

Let’s see the usage of the pandas series plot() function to display a line chart of the series values.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

employee_counts.plot()

Output:

Line plot from the "employee_counts" series

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.

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")

Output:

Line chart with custom formatting

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 –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top