Line Chart in Machine Learning

Omkar Raut
Machine Learning Concepts
5 min readJun 10, 2021

In this post, I have tried to explain to you the implementation of a line chart using libraries and tools.

We have seen all types of methods to visualize data and tools to make it in the last post. In this post, I am going to implement line charts using libraries in python and in Tableau also. If we want to show the progress of one quantity over the years or over the months then we can go ahead with a line chart. So let’s start with the implementation.

Before going to implement a line chart using different methods, I will introduce you to the dataset that I have. I have a dataset of 2021/W2: Gender Inequality in HIV Infections in Adolescents. you can download it from https://data.world/makeovermonday/2021w2 .

Let’s load the dataset in the jupyter notebook, and then will start with the implementation of a line chart.

I have used the pandas library to load the data. We can see in the dataset that there are many countries. We have to create a line graph of each country and the Estimated number of people living with HIV year-wise. So, first of all, we have to identify the countries. So, let’s do it first.

Each country has the count of HIV active patients year-wise from 1990 to 2019. Let’s go ahead with the implementation.

1) Line Chart Using mathplotlib:

Mathplotlib is a data visualization library used in python. It has many functions and the plot() function is used to plot a line chart. plot() function takes at least two-argument such as x-axis data and y-axis data. Of course, we can provide more arguments. You can check out the all arguments here: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html

Let’s create a line chart for the Estimated number of people living with HIV in all provided countries year-wise, separate for males and females.

So, this type of visualization we will get by using the mathplotlib library. Now, let’s go ahead with seaborn for the line graph.

2) Line Chart using Seaborn library

The seaborn is also a visualization library used in python. But to give a title, and to show the graph, we need the mathplotlib library. You will get this point after seeing the code given below. We are using the lineplot() function which is in the seaborn library. This function takes three required parameters such as data, x-axis attribute, and y-axis attribute. Here we have to put all the data that we have and just put the name of attributes for the x, and y-axis. Also, there are some more parameters we can use. If you want to know more visit the link: https://seaborn.pydata.org/generated/seaborn.lineplot.html

Let’s create a line chart for the Estimated number of people living with HIV in all provided countries year-wise, separate for males and females using the seaborn library.

This is what I got after executing the above code. Let’s move ahead to build a line chart using another library.

3) Line Chart using Plotly:

Plotly is another visualization library used in python. I am using the line() function which is in the plotly library to draw the line chart. There are also some parameters we have to put in the function line() such as data, x-axis attribute, y-axis parameter, and there are many others. You can check out at: https://plotly.com/python/line-charts/

Let’s create a line chart for the Estimated number of people living with HIV in all provided countries year-wise, separate for males and females using the plotly library.

The main advantage of plotly is that you will get a dynamic graph. You can just know more information about the graph by just hovering the cursor on the graph. Also, we can download the particular graph as a picture. Let’s move ahead and develop a visualization using a tool called Tableau.

4) Line Chart using Tableau:

We will not require any coding knowledge to design the visualization using Tableau. Let’s do it.

This is a screenshot of the Tableau dashboard. It is fully dynamic. When we select the country in the left window, the right window will display the line graph for the particular country. I recommend you to visit my dashboard and see it. I hope you will like it. It is here: https://public.tableau.com/shared/ZDD2D4YJ5?:display_count=n&:origin=viz_share_link

I have put the above code in my Github repository. You can check it out and make changes to it.

Get a code of this post.

Conclusion

In this particular post, I have introduced you to a line chart and how to implement it using libraries and the Tableau tool which is most popular for data visualization. I have also provided the code and the link to the tableau dashboard.

Thank you.

--

--

Omkar Raut
Machine Learning Concepts

Bachelor of Technology in Computer Engineer, at Dr. Babasaheb Ambedkar Technological University, Lonere, Raigad, India.