Python Data Visualisation Libraries: How to Choose the Right One for Your Data Analysis Needs
Data visualisation is the process of creating visual representations of data using the Python programming language. This can include creating charts, plots, and graphs to help understand and analyze data. Python has a wide range of libraries and tools available for data visualization, such as Matplotlib, Seaborn, Plotly, and Bokeh, that make it easy to create interactive and visually appealing data visualizations. These libraries provide a variety of customizable options for creating visualizations such as bar charts, line plots, scatter plots, and heat maps. Python data visualisation is widely used in the field of data science, business intelligence, and scientific research to gain insights from data and make data-driven decisions.
As a Data analyst, we responsible for collecting, analysing, and interpreting large sets of data. The goal of a data analyst is to extract valuable insights from data and to provide recommendations to the company based on those insights and to provide that an analyst uses various visualisation tools and libraries to create a dashboard or charts to forecast the growth of the organisation or product.
Here, we will see how python as a visualisation tool is widely used by data analysts in their day to day life.
Some popular Python data visualisation libraries include:
- Pandas: This is a Python library for data manipulation and analysis. It provides data structures and data analysis tools for handling and manipulating numerical tables and time series data. It is widely used in data science and machine learning projects, as well as in finance, economics, and statistics.
- Matplotlib: This is a widely used library for creating static, animated, and interactive visualisations. It provides a wide range of customisable options for creating visualisations such as bar charts, line plots, scatter plots, and heat maps.
- Seaborn: This library is built on top of Matplotlib and is designed for creating more attractive and informative statistical graphics. It has a higher-level interface for creating visualisations, making it more user-friendly than Matplotlib.
- Plotly: This library allows for creating interactive and online visualisations. It supports a wide range of chart types such as scatter plots, bar charts, line charts, heat maps, and 3D plots.
- Bokeh: This library is also built for creating interactive visualisations, which can be rendered in web browsers. It is useful when creating visualisations that need to be embedded in a web page or an application.
- ggplot: This library is a port of R’s ggplot2 library and is used for creating complex visualisations. It is built on top of Matplotlib and has a different syntax and structure than other libraries.
- Plotly Express: This is a high-level interface of plotly and is built on top of Plotly. It is designed for creating simple and easy-to-create visualisations in a single line of code.
- Geopandas and Folium: These libraries are used for creating geographic visualisations, such as choropleth maps, heat maps, and point maps.
- NetworkX and Pyvis: These libraries are used for creating network visualisations such as graphs, and for visualising data in the form of nodes and edges.
But now ,the question lies when ? where ? and how ? to use these various libraries according to our need and to gain profitable insights from our data.
Let’s see,
- Pandas: It is a powerful library for data manipulation and analysis, and it also has built-in visualization capabilities through the use of Matplotlib, a popular data visualization library in Python. Pandas provides the
DataFrame.plot()
method, which is a wrapper around Matplotlib'spyplot.plot()
method. This makes it very easy to create visualizations directly from a Pandas DataFrame.
import pandas as pd
import matplotlib.pyplot as plt
data = {'x': [1, 2, 3, 4, 5], 'y': [2, 4, 6, 8, 10]}
df = pd.DataFrame(data)
df.plot(x='x', y='y', kind='bar')
plt.show()
The above code creates a DataFrame from a dictionary of data, then uses the .plot()
method to create a line plot of the 'x' and 'y' columns. The kind
parameter is used to specify the type of plot, in this case, 'line'.
You can also use the DataFrame.plot.bar()
method to create a bar chart, or the DataFrame.plot.scatter()
method to create a scatter plot.
Pandas also provides the DataFrame.plot.hist()
method to create a histogram and DataFrame.plot.box()
method to create a box plot, you can also use df.hist()
and df.box()
respectively to create the plots.
For more advanced visualizations, you can use the DataFrame.plot()
method in conjunction with Matplotlib's customisation options.
df.plot(x='x', y='y', kind='line', color='red', linewidth=2)
plt.title('Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
- Matplotlib: Matplotlib is a 2D plotting library for creating static, animated, and interactive visualisations. It provides a wide range of customizable options for creating visualizations such as bar charts, line plots, scatter plots, and heat maps. Here’s an example of creating a simple line plot using Matplotlib:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.show()
- Seaborn: Seaborn is built on top of Matplotlib and is designed for creating more attractive and informative statistical graphics. It has a higher-level interface for creating visualizations, making it more user-friendly than Matplotlib. Here’s an example of creating a simple scatter plot using Seaborn:
import seaborn as sns
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
sns.scatterplot(x, y)
- Plotly: Plotly allows for creating interactive and online visualizations. It supports a wide range of chart types such as scatter plots, bar charts, line charts, heatmaps, and 3D plots. Here’s an example of creating a simple bar chart using Plotly:
import plotly.express as px
data = {'x': [1, 2, 3, 4, 5], 'y': [2, 2.5, 3, 5, 14]}
fig = px.bar(data, x='x', y='y')
fig.show()
- Bokeh: Bokeh is built for creating interactive visualizations, which can be rendered in web browsers. It is useful when creating visualizations that need to be embedded in a web page or an application. Here’s an example of creating a simple scatter plot using Bokeh:
from bokeh.plotting import figure, show
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
p = figure(title='Simple Scatter Plot')
p.scatter(x, y)
show(p)
- Plotly Express: Plotly Express is a high-level interface of plotly and is built on top of Plotly. It is designed for creating simple and easy-to-create visualizations in a single line of code. Here’s an example of creating a simple scatter plot using Plotly Express:
import plotly.express as px
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
fig = px.scatter(x=x, y=y)
fig.show()
- Geopandas and Folium: Geopandas and Folium are libraries used for creating geographic visualizations, such as choropleth maps, heatmaps, and point maps. Here’s an example of creating a simple choropleth map using Geopandas and Folium:
import geopandas as gpd
import folium
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
m = folium.Map(location=[world.centroid.y.mean(), world.centroid.x.mean()], zoom_start=1)
folium.Choropleth(geo_data=world, data=world, columns=['iso_a3', 'gdp_md_est'], key_on='feature.id', fill_color='YlGn').add_to(m)
m
- NetworkX and Pyvis: NetworkX and Pyvis are libraries used for creating network visualizations such as graphs, and for visualizing data in the form of nodes and edges. Here’s an example of creating a simple graph visualization using NetworkX and Pyvis:
import networkx as nx
from pyvis.network import Network
G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3), (2, 3), (3, 4), (4, 5), (4, 6)])
net = Network(notebook=True)
net.from_nx(G)
net.show("example.html")
These are just a few examples of how to use Libraries for data visualisation. With the combination of different library, you can create a wide range of visualisations to help you better understand and analyse your data.
— — Please note that the code above is just an example and you may need to install the libraries in order to run the code. — —
Conclusion
Data visualisation is an important aspect of data analysis and decision making. Python offers a wide range of libraries and tools for creating visually appealing and informative visualisations. From basic plots to more advanced interactive visualisations, Python has something to offer for every data visualisation need.
This is why you should always keep brushing up your python skills cause its a never ending learning process. In this article , I summarised few libraries with examples — some of them by experience but there are way more that are frequently used as a data analyst , data scientist and many other data-related fields.