Line plot or Line chart in Python with Math

Jahid Hasan
3 min readJul 15, 2019

--

In this article, I will discuss the Line plot for data visualization. Now going to simple statistics math for more details.

Suppose, the Arctic is a hospital name. The birth rate for the last week was given below in this hospital.

Make a Line plot for birth rate data.

Visualizing Line plot with Matplotlib

Now that you’ve seen how to build a Line plot in Python. We have to use Matplotlib and Pandas library for visualization.

>>> import matplotlib.pyplot as plt
>>> day = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
>>> baby = [15,13,17,9,11,19,13]
>>> plt.plot(day, baby, color='Blue')
>>> plt.xlabel('Day')
>>> plt.ylabel('Birth Rate')
>>> plt.title('Arctic hospital last week birth rate')
>>> plt.show()

Multiple Line chart:

If you want a multiple line chart in one plot. Suppose, baby = [15,13,17,9,11,19,13] these data are from July 1, 2019 to July 7, 2019. Now we will add some data from July 8, 2019 to July 14, 2019 and create a new table.

Make a multiple Line plot for birth rate data.

Now we going to our coding part. We add the legend for line_chart1 and line_chart2.

>>> import matplotlib.pyplot as plt
>>> day = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
>>> baby_one = [15,13,17,9,11,19,13]
>>> baby_two = [12,10,18,14,16,20,7]
>>> line_chart1 = plt.plot(day, baby_one, color='Blue')
>>> line_chart2 = plt.plot(day, baby_two, color='Red')
>>> plt.xlabel('Day',color="green")
>>> plt.ylabel('Birth Rate',color="green")
>>> plt.title('Arctic hospital last week birth rate')
>>> plt.legend(['1-7 July 2019', '8-14 July 2019'], loc=3)
>>> plt.show()

Multiple line chart with different line style:

You can easily add line style in the line chart. For more details please visit this link https://matplotlib.org/2.1.2/api/_as_gen/matplotlib.pyplot.plot.html

>>> import matplotlib.pyplot as plt
>>> day = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
>>> baby_one = [15,13,17,9,11,19,13]
>>> baby_two = [12,10,18,14,16,20,7]
>>> line_chart1 = plt.plot(day, baby_one,ls= '--', color='Blue')
>>> line_chart2 = plt.plot(day, baby_two,ls= '-.', color='Red')
>>> plt.xlabel('Day',color="green")
>>> plt.ylabel('Birth Rate',color="green")
>>> plt.title('Arctic hospital last week birth rate')
>>> plt.legend(['1-7 July 2019', '8-14 July 2019'], loc=3)
>>> plt.show()

Line chart in Pandas Library:

Also, you can create a line chart by the Pandas library. It’s also easy to stuff. Let’s do it,

>>> import pandas as pd
>>> df = pd.DataFrame({
'baby_one': [15,13,17,9,11,19,13],
'baby_two': [12,10,18,14,16,20,7]
}, index=['Mon','Tue','Wed','Thu','Fri','Sat','Sun'])
>>> df.plot.line()

That’s it. Thank you :)

--

--

Jahid Hasan

Enthusiast of Data Science | Data Analyst | Machine Learning | Artificial intelligence | Deep Learning | Author || ps: https://msjahid.github.io/