Linkedin Network Visualisation using Plotly | by Saiteja Kura | Towards Data Science

For many years Linkedin has been the go-to platform for building a network and premium job searching. People land in their dream jobs and internships by establishing connections on this platform. Most of us have many connections on Linkedin. Let us try and visualize these connections.
I also believe Linkedin connection visualization is the best case for illustrating the usage of TreeMaps. We will be using the Pandas library for data extraction and the Plotly library for data Visualization. I find Plotly to be one of the most useful and interactive plotting libraries available in Python. In Addition, Plotly’s ability to integrate with Dash Framework(which is used to build web-apps) makes it even more unique. We can even share Plotly plots online.
NOTE : The tree maps in this article are interactive. Please view the GIPHY’s to understand the interactivity.
Extraction of Linkedin connections data can be done in two ways. One being the Linkedin API which requires multiple permissions for accessing data and its usage is not straight-forward. The other being directly downloading the .csv file from our Settings page on Linkedin.
Let us first import the Pandas and Plotly Library.
import pandas as pdimport plotly.express as pximport plotly.graph_objects as go
Now, let us read the Connections file which was downloaded from LinkedIn and try to understand its structure.
As you can see in the above picture, Each row lists the First Name, Last Name, Email Address, Company, Position, and the date of connection. You can also notice that there are 335 rows in total which indicates I have 335 connections. One anomaly which can be noticed is the type of Connected On column which is an object instead of type date. The data is also arranged in such a way that the most recent connection is placed first and so on.
Now let us try and draw a line plot illustrating the growth in the number of connections wrt. Time. But we need to sort the data frame in ascending order of date and draw a line plot.
Firstly let us convert the Connected On column to date type and draw a line plot by sorting the data frame date-wise.
import datetimedef convert(date):return datetime.datetime.strptime(date, "%d %b %Y").strftime("%Y-%m-%d")df["Connected On"] = df["Connected On"].apply(convert)
The apply function applies the function convert on each date value in the Connected On Column.
df=df.sort_values(by="Connected On")
Let us draw the line plot using the Plotly library.
Note — Before drawing the plots the data frame needs to be modified a bit. Do check my GitHub code. The link is given at the end of this article.
connections_line = px.line(df, x="Connected On", y="number", title='My Connections')connections_line.show()
As I mentioned, Plotly is known for its interactive nature we can see in the above plot that when we hover on the line, the exact date and number of connections on that day can be seen.
Now, we will be analyzing our connections in two ways. One being the company-wise analysis which depicts the details of connections segregated by company and other being the position-wise analysis which depicts the details of connections segregated by position.
Let us draw histograms to illustrate the company-wise and Position wise distribution of our connections.
# Company wise histogramfig = go.Figure()fig.add_trace(go.Histogram(histfunc="count",  x=df["Company"]))fig.show()# Position wise histogramfig = go.Figure()fig.add_trace(go.Histogram(histfunc="count",  x=df["Position"]))fig.show()
The histfunc argument takes an aggregate function as a parameter. Here we pass the count as the parameter to count the number of rows wrt. each company(Identifying the frequency of each unique value).
While hovering on the bars we can notice the Company/Position name and their corresponding values being mentioned.
The one drawback of histograms is that we cannot get the complete picture of the company and position details in one plot itself. Hence, TreeMaps come into the picture.
TreeMaps are used to depict the hierarchical structure. A TreeMap is made up of rectangles that are sized and ordered by a quantitative variable.
Each category is assigned a rectangle and sub-categories in that category are represented as nested rectangles. The size of each rectangle is in proportion to the count of that quantity. More the quantity, larger the rectangle. For Example, in the histogram above we see that Cotiviti took the first place in the Company-wise histogram, and Intern took the first place in the position-wise histogram. Hence we can expect these two values to have the largest rectangles in the TreeMap. Here is another example to clearly understand a TreeMap.
Now let us plot a TreeMap based on Company and based on Position. Since Plotly is an interactive library we can zoom in to certain parts of the TreeMap too.
We pass the tree hierarchy to the path argument in px.treemap() function.
company_tree_map = px.treemap(df, path=['My Network', 'Company', 'Position'], width=1000, height=1000)company_tree_map.show()
We can notice the company Cotiviti occupying the largest area in the TreeMap(Cotiviti had the highest values in the histogram too!). The specialty of TreeMaps in Plotly is that we can zoom in to a certain subtree to have an intelligible look at the sub rectangles. In this TreeMap the sub-rectangles are the variety of roles in each company(only in my connections). We can also notice that whenever we hover we can see two properties.1. Parent — which denotes the parent of the corresponding rectangle2. Id — which denotes the hierarchical structure of that rectangle.
position_tree_map = px.treemap(df, path=['My Network', 'Position', 'Company'], width=1000, height=1000)position_tree_map.show()
We can notice the Intern Position occupying the largest area in the TreeMap(Intern had the highest values in the histogram too!). Here the sub-rectangles represent various companies corresponding to each position.
The full code can be found in Github here.
I visualized the Linkedin network using Plotly Library and also felt this was the best use case to illustrate the usage of TreeMaps. I hope you took home some concepts today. Do feel free to share feedback. Stay Home and Stay Safe!