Training Outcomes Within Your Budget!

We ensure quality, budget-alignment, and timely delivery by our expert instructors.

Share this Resource

Table of Contents

Python Data Visualisation

Data Visualisation is a crucial aspect of data analysis, allowing us to present complex information in a visually appealing and understandable manner. Python, with its rich ecosystem of libraries, offers a plethora of tools to create stunning Data Visualisations. Python Data Visualisation has become an essential tool for data scientists and analysts to utilise.  

According to the Python website, there are over 137,000 libraries available on Python, most of which support Data Visualisation. In this blog, we will explore the world of Python Data Visualisation and guide you towards effectively presenting data using various visualisation techniques. 

Table of Contents 

1) Introduction to Python Data Visualisation 

2) Customising Plots with Matplotlib 

3) Creating Interactive Visualisations with Plotly 

4) Visualising Data with Seaborn 

5) Geospatial Data Visualisation with Geopandas 

6) Network Visualisation with NetworkX 

7) Conclusion 

Introduction to Python Data Visualisation 

Data Visualisation is a powerful technique that transforms complex data into meaningful and insightful visual representations, allowing us to grasp patterns, trends, and relationships that might otherwise remain hidden in raw data. Python, with its extensive collection of Data Visualisation libraries, has emerged as a go-to language for creating stunning and interactive visualisations. 

Python Data Visualisation libraries, such as Matplotlib, Plotly, Seaborn, and Pandas, provide a wide range of tools and capabilities to create various types of visualisations, from simple line plots to sophisticated 3D visualisations. These libraries offer user-friendly interfaces, making it easier for data analysts, scientists, and researchers to produce informative plots with minimal effort. 

Moreover, Python's versatility and integration with other data science libraries make it an excellent choice for Data Visualisation tasks in diverse domains such as finance, healthcare, marketing, and more. Whether you need to explore data for exploratory analysis or communicate insights to stakeholders, Python Data Visualisation tools are indispensable in modern data-driven decision-making. 

In this digital age, data is being generated at an unprecedented rate. To harness the full potential of data, effective Data Visualisation is paramount. Python Data Visualisation empowers individuals and businesses to tell compelling data stories, leading to better understanding, decision-making, and, ultimately, success in today's data-driven world. There are several excellent tools available for Data Visualisation in Python. However, we will focus on Matplotlib, Plotly, Seaborn, Geopandas and NetworkX to cover a wide variety of visualisation options.
 

Programming Training
 

Customising Plots with Matplotlib 

Matplotlib is a 2D plotting library that enables the creation of high-quality static, interactive, and animated visualisations in Python. It was developed by John D. Hunter in 2003 as a tool to replicate MATLAB's plotting capabilities in Python. Over the years, Matplotlib has become an integral part of the data science ecosystem, making Data Visualisation accessible and straightforward. 

Installation and Getting Started 

Before using Matplotlib, it is essential to install the library. If you haven't installed it yet, you can do so using the following command: 

pip install matplotlib 

Once installed, you can import Matplotlib into your Python script or Jupyter Notebook using the following convention: 

import matplotlib.pyplot as plt 

Line Plot 

The line plot is one of the simplest and most used plots in Matplotlib. It is used to visualise the relationship between two variables, typically representing continuous data points. To create a line plot, you need to provide x and y data points, which are then connected by straight lines. 
 

import matplotlib.pyplot as plt 

x = [1, 2, 3, 4, 5] 

y = [10, 15, 7, 20, 12] 

plt.plot(x, y) 

plt.xlabel('X-axis') 

plt.ylabel('Y-axis') 

plt.title('Line Plot') 

plt.show() 


Scatter Plot 

Scatter plots are useful for visualising the distribution and relationship between two variables. Instead of connecting the data points with lines, scatter plots represent each data point as a dot, allowing you to identify patterns and correlations easily.
 

import matplotlib.pyplot as plt 

x = [1, 2, 3, 4, 5] 

y = [10, 15, 7, 20, 12] 

plt.scatter(x, y) 

plt.xlabel('X-axis') 

plt.ylabel('Y-axis') 

plt.title('Scatter Plot') 

plt.show() 


Bar Plot 

Bar plots are widely used for visualising categorical data or comparing different categories. They are effective in representing discrete data points and can be used to show counts, frequencies, or percentages. 
 

import matplotlib.pyplot as plt  

categories = ['A', 'B', 'C', 'D']  

values = [20, 30, 15, 25]  

plt.bar(categories, values)  

plt.xlabel('Categories')  

plt.ylabel('Values')  

plt.title('Bar Plot')  

plt.show() 


Histogram 

Histograms are used to visualise the distribution of continuous data and identify the frequency of data points falling within specific bins or intervals. 

  

import matplotlib.pyplot as plt  

data = [10, 20, 15, 30, 25, 40, 35, 50]  

plt.hist(data, bins=5)  

plt.xlabel('Data')  

plt.ylabel('Frequency')  

plt.title('Histogram')  

plt.show() 

 

Pie Chart 

Pie charts are ideal for illustrating the proportion of different categories within a dataset. Each category is represented as a slice of the pie, with the size of the slice corresponding to its percentage of the whole. 

  

import matplotlib.pyplot as plt  

categories = ['A', 'B', 'C', 'D']  

sizes = [25, 30, 15, 30]  

plt.pie(sizes, labels=categories, autopct='%1.1f%%')  

plt.title('Pie Chart')  

plt.show() 

 

Box Plot 

Box plots, also known as box-and-whisker plots, are useful for visualising the distribution and identifying outliers in a dataset. 
 

import matplotlib.pyplot as plt 

data = [10, 20, 15, 30, 25, 40, 35, 50]  

plt.boxplot(data)  

plt.title('Box Plot')  

plt.show() 

Matplotlib is a versatile Data Visualisation library that empowers Python users to create a wide range of plots, from simple line plots to complex visualisations. In this article, we explored some of its commonly used plots, including line plots, scatter plots, bar plots, histograms, pie charts, and box plots. By mastering Matplotlib's functionalities, you can effectively present and analyse data, making it an indispensable tool in the data scientist's arsenal. 

Unlock your coding potential by joining our Python Programming Course! 

Creating Interactive Visualisations with Plotly 

Plotly is an open-source Data Visualisation library that provides an interactive and easy-to-use interface for creating a wide range of visualisations, including line charts, scatter plots, bar charts, 3D plots, choropleth maps, and more. Developed by Plotly Inc., the library is widely used in various industries, from finance and healthcare to marketing and academia. 

Plotly can be used as both an offline and online tool. In offline mode, the visualisations are rendered within the Jupyter Notebook or Python script, allowing for seamless integration with data analysis workflows. In contrast, Plotly's online mode enables users to store and share interactive visualisations on the Plotly cloud platform. 

Getting Started with Plotly 

Before diving into interactive visualisations, it's essential to install the Plotly library. If you haven't installed it yet, use the following command: 

pip install plotly 

To begin using Plotly, import the library as follows: 

import plotly.graph_objects as go   

Interactive Line Chart 

Line charts are ideal for visualising trends and changes over time. With Plotly, creating an interactive line chart is straightforward. You can customise the chart by adding titles, labels, and annotations to enhance its visual appeal and readability.

 

import plotly.graph_objects as go  

x = [1, 2, 3, 4, 5]  

y = [10, 15, 7, 20, 12]  

fig = go.Figure()  

fig.add_trace(go.Scatter(x=x, y=y, mode='lines+markers', name='Line Chart'))  

fig.update_layout(title='Interactive Line Chart', xaxis_title='X-axis', yaxis_title='Y-axis')  

fig.show() 


Interactive Scatter Plot 

Scatter plots are used to visualise the relationship between two variables. In an interactive scatter plot, hovering over data points reveals additional information, making it easier to explore individual data points.
 

import plotly.graph_objects as go  

x = [1, 2, 3, 4, 5]  

y = [10, 15, 7, 20, 12]  

fig = go.Figure()  

fig.add_trace(go.Scatter(x=x, y=y, mode='markers', name='Scatter Plot'))  

fig.update_layout(title='Interactive Scatter Plot', xaxis_title='X-axis', yaxis_title='Y-axis')  

fig.show() 


Interactive Bar Chart 

Bar charts are effective in comparing categorical data. In an interactive bar chart, you can hover over bars to view specific values, providing a dynamic way to analyse data.
 

import plotly.graph_objects as go  

categories = ['A', 'B', 'C', 'D']  

values = [20, 30, 15, 25]  

fig = go.Figure()  

fig.add_trace(go.Bar(x=categories, y=values, name='Bar Chart'))  

fig.update_layout(title='Interactive Bar Chart', xaxis_title='Categories', yaxis_title='Values')  

fig.show() 


Interactive 3D Plot 

Plotly also allows users to create interactive 3D visualisations. This is especially useful for visualising complex data and relationships in a three-dimensional space. 

 

import plotly.graph_objects as go  

import numpy as np  

x = np.random.rand(100)  

y = np.random.rand(100)  

z = np.random.rand(100)  

fig = go.Figure()  

fig.add_trace(go.Scatter3d(x=x, y=y, z=z, mode='markers', marker=dict(size=5))) 

fig.update_layout(title='Interactive 3D Plot', scene=dict(xaxis_title='X-axis', yaxis_title='Y-axis', zaxis_title='Z-axis'))  

fig.show()

Plotly is a powerful Data Visualisation library that empowers Python users to create interactive and engaging visualisations. Its wide range of capabilities, including line charts, scatter plots, bar charts, and 3D plots, enables data analysts and scientists to effectively present and explore data in a dynamic and interactive manner. By leveraging Plotly's features, users can gain deeper insights and communicate complex data patterns with ease. 

Master web development with Python Django Training - Build dynamic and robust web applications with ease! 

Visualising Data with Seaborn 

Seaborn is built on top of Matplotlib, another popular Data Visualisation library in Python. While Matplotlib provides a powerful foundation for creating static visualisations, Seaborn extends its capabilities by offering a higher-level interface with more sophisticated and visually appealing statistical graphics. With Seaborn, you can easily create complex visualisations with minimal effort, making it a favourite choice among data analysts and scientists. 

Installing Seaborn 

Before using Seaborn, ensure that you have it installed. If you haven't installed it yet, you can do so using the following command: 

pip install seaborn 

Once installed, you can import Seaborn in your Python script or Jupyter Notebook: 

import seaborn as sns   

Scatter Plot 

Scatter plots are useful for visualising the relationship between two continuous variables. Seaborn's scatterplot function makes it easy to create scatter plots with additional features like colour-coding data points based on a categorical variable.

 

import seaborn as sns  

import matplotlib.pyplot as plt  

# Sample data  

x = [1, 2, 3, 4, 5]  

y = [10, 15, 7, 20, 12] 

category = ['A', 'B', 'C', 'A', 'B']  

sns.scatterplot(x=x, y=y, hue=category)  

plt.title('Scatter Plot with Seaborn')  

plt.show() 

 

Bar Plot 

Bar plots are used to visualise categorical data or to compare categories. Seaborn's barplot function allows you to create bar plots with ease.
 

import seaborn as sns  

import matplotlib.pyplot as plt  

# Sample data  

categories = ['A', 'B', 'C', 'D']  

values = [20, 30, 15, 25]  

sns.barplot(x=categories, y=values)  

plt.title('Bar Plot with Seaborn')  

plt.show() 


Histogram 

Histograms are useful for visualising the distribution of a single variable. Seaborn's distplot function combines a histogram with a kernel density plot, providing insights into the data's underlying distribution.
 

import seaborn as sns  

import matplotlib.pyplot as plt  

  

# Sample data  

data = [10, 20, 15, 30, 25, 40, 35, 50]  

 

sns.distplot(data)  

plt.title('Histogram with Seaborn')  

plt.show() 


Box Plot 

Box plots are ideal for visualising the distribution of data and identifying outliers. Seaborn's boxplot function makes it simple to create box plots with additional features like grouping data based on a categorical variable. 

 

import seaborn as sns 

import matplotlib.pyplot as plt 

# Sample data 

categories = ['A', 'B', 'A', 'B', 'C', 'C'] 

values = [10, 20, 15, 30, 25, 40] 

sns.boxplot(x=categories, y=values) 

plt.title('Box Plot with Seaborn') 

plt.show() 


Heatmap 

Heatmaps are used to visualise data in a tabular form, with colours representing the data's intensity. Seaborn's heatmap function allows you to create heatmaps to explore correlations or patterns in large datasets. 
 

import seaborn as sns  

import matplotlib.pyplot as plt  

import numpy as np  

# Sample data  

data = np.random.rand(10, 10)  

sns.heatmap(data, annot=True, cmap='YlGnBu')  

plt.title('Heatmap with Seaborn')  

plt.show() 


Customising Seaborn Plots 

Seaborn allows you to customise plots easily. You can change colours, styles, and labels to suit your preferences or match your data's presentation requirements.
 

import seaborn as sns  

import matplotlib.pyplot as plt  

# Sample data  

x = [1, 2, 3, 4, 5]  

y = [10, 15, 7, 20, 12]  

# Create a scatter plot with customised colours and style  

sns.scatterplot(x=x, y=y, color='purple', marker='s', s=100, label='Data Points') 

# Add title and labels  

plt.title('Customised Scatter Plot with Seaborn')  

plt.xlabel('X-axis')  

plt.ylabel('Y-axis')   

# Show legend  

plt.legend()  

plt.show()


Seaborn is a versatile and powerful Data Visualisation library that complements Matplotlib by providing a higher-level interface for creating attractive and informative visualisations. Its simplicity and aesthetic appeal make it a preferred choice among data analysts and scientists. In this article, we explored some commonly used plots in Seaborn, including scatter plots, bar plots, histograms, box plots, and heatmaps.  

Unlock the world of coding with our Introduction to Programming with Python and Java Training! 

Geospatial Data Visualisation with Geopandas 

Geopandas is an open-source library built on top of Pandas, making it compatible with Pandas' DataFrame structure. This allows users to integrate geospatial data seamlessly with traditional tabular data and leverage the rich set of data manipulation functions offered by Pandas. 

Geopandas simplifies the handling of geospatial data by providing a unified data structure called a "GeoDataFrame." A GeoDataFrame is essentially a Pandas DataFrame with an additional "geometry" column, which stores the geometric data for each spatial feature, such as points, lines, or polygons. 

Installing Geopandas 

Before using Geopandas, you need to install it along with its dependencies. Depending on your operating system, you may need to install additional geospatial libraries for proper functionality. For most users, the following command will suffice: 

pip install geopandas 

Additionally, you can install Geopandas' optional dependencies for more advanced geospatial functionalities: 

pip install geopandas[all] 

Getting Started with Geopandas 

Once Geopandas is installed, you can import it and begin exploring geospatial data. 

import geopandas as gpd 

Geopandas can read various geospatial file formats, including Shapefiles, GeoJSON, and Geopackage. To read a geospatial file, use the read_file function:

 

# Read a Shapefile  

gdf = gpd.read_file('path/to/shapefile.shp')  

# Read a GeoJSON file  

gdf = gpd.read_file('path/to/geojsonfile.geojson')  

# Read a Geopackage file  

gdf = gpd.read_file('path/to/geopackagefile.gpkg') 


Geospatial Data Visualisation 

Geopandas leverages the plotting functionalities of Matplotlib to create geospatial visualisations. By calling the plot function on a GeoDataFrame, you can create various types of geospatial visualisations, such as point maps, choropleth maps, and more.
 

import geopandas as gpd 

import matplotlib.pyplot as plt 

# Read a GeoJSON file 

gdf = gpd.read_file('path/to/geojsonfile.geojson') 

# Create a point map 

gdf.plot(marker='o', color='red', markersize=5) 

plt.title('Geospatial Point Map') 

plt.show() 

python 

Copy code 

# Create a choropleth map 

gdf.plot(column='population', cmap='OrRd', legend=True) 

plt.title('Choropleth Map of Population') 

plt.show() 


Geospatial Data Analysis 

Geopandas enables powerful geospatial data analysis by providing spatial operations and manipulations. You can perform spatial queries, such as finding points within a polygon or intersecting geometries, using the built-in spatial methods.
 

import geopandas as gpd  

# Read a GeoJSON file of countries  

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))  

# Find countries that intersect a specific point  

point = gpd.GeoSeries([Point(-74.0071, 40.7128)])  

countries = world[world.intersects(point.unary_union)]  

# Output the intersecting countries' names  

print(countries['name'])


Geopandas is a powerful and user-friendly Python library that simplifies geospatial data manipulation and visualisation. By combining the capabilities of Pandas with geospatial data structures, Geopandas provides data analysts and scientists with an efficient way to handle geospatial data and gain insights through geospatial visualisations and analysis. Whether you are working with point data, lines, or polygons, Geopandas offers a seamless integration with Pandas, making it an invaluable tool for anyone dealing with geospatial datasets. Its ease of use and powerful functionalities have made it a go-to for geospatial data analysis and visualisation in the Python ecosystem. 

Unlock the Power of Data: Join our Data Analysis and Visualisation with Python Course Now! 

Network Visualisation with NetworkX 

Networks, also known as graphs, are a powerful way to represent and analyse complex relationships between interconnected entities. NetworkX is a Python library that provides tools for creating, visualising, and analysing networks. With NetworkX, users can explore various types of networks, such as social networks, transportation networks, biological networks, and more. It is built on top of Python's data science stack, making it easy to integrate with other data analysis and visualisation libraries like NumPy, Pandas, and Matplotlib. 

NetworkX allows users to create, manipulate, and study the structure, dynamics, and functions of complex networks. It supports directed and undirected graphs, as well as multi-graphs, which can have multiple edges between the same pair of nodes. NetworkX also offers various algorithms for network analysis, such as centrality measures, shortest path algorithms, and community detection. 

Installing NetworkX 

To get started with NetworkX, you need to install it. You can install NetworkX using pip: 

pip install networkx 

Once installed, you can import NetworkX in your Python script or Jupyter Notebook: 

import networkx as nx 

Creating Networks with NetworkX 

NetworkX provides straightforward methods for creating different types of networks. You can start with an empty graph and add nodes and edges or use NetworkX's built-in functions to generate specific types of networks. 

Creating an Empty Graph 
 

import networkx as nx  

  

G = nx.Graph()  # Undirected graph  

G = nx.DiGraph()  # Directed graph 


Adding Nodes and Edges 
 

import networkx as nx  

   

G = nx.Graph()  

  

G.add_node(1)   

G.add_nodes_from([2, 3, 4])  

G.add_edge(1, 2)  

G.add_edges_from([(1, 3), (2, 4)]) 


Generating Specific Networks 
 

import networkx as nx  

G = nx.random_graphs.erdos_renyi_graph(10, 0.3)  # Erdos-Renyi random graph  

G = nx.random_graphs.barabasi_albert_graph(10, 2)  # Barabasi-Albert preferential attachment graph  

G = nx.random_graphs.watts_strogatz_graph(10, 2, 0.3)  # Watts-Strogatz small-world graph 


Network Visualisation with NetworkX and Matplotlib 

Visualising networks is essential for gaining insights into their structures and characteristics. NetworkX integrates seamlessly with Matplotlib, allowing you to create visual representations of networks. 
 

import networkx as nx  

import matplotlib.pyplot as plt  

# Create a graph  

G = nx.Graph()  

G.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 1)])  

# Draw the graph  

pos = nx.spring_layout(G)  # Position nodes using the spring layout algorithm  

nx.draw(G, pos, with_labels=True, node_size=1000, node_color='skyblue', font_size=12, font_weight='bold')  

# Show the plot 

plt.title('Network Visualisation with NetworkX')  

plt.show() 

Network Analysis with NetworkX 

NetworkX provides various algorithms and metrics for network analysis. You can compute centrality measures, shortest paths, and community structures, among other things. 

 

import networkx as nx  

  

# Create a graph  

G = nx.Graph()  

G.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 1)])  

   

# Calculate degree centrality  

degree_centrality = nx.degree_centrality(G)  

print(degree_centrality)  

  

# Calculate shortest path  

shortest_path = nx.shortest_path(G, source=1, target=4)  

print(shortest_path)  

  

# Detect communities using Louvain method  

communities = nx.algorithms.community.modularity_max.greedy_modularity_communities(G)  

print(communities) 


NetworkX is a powerful Python library for working with networks and graph-related data. It enables users to create, visualise, and analyse complex networks efficiently. With its seamless integration with other Python libraries, such as Matplotlib, NumPy, and Pandas, NetworkX provides a versatile toolset for network analysis and visualisation. Whether you are exploring social networks, biological networks, or any other interconnected data, NetworkX offers a user-friendly interface to study the underlying structures and relationships of your data. 

Level up your programming skills with our comprehensive Programming Training today! 

Conclusion 

Data Visualisation is an indispensable part of data analysis, and Python offers a wide array of libraries to create impactful visualisations. Throughout this blog post, we have explored various Python Data Visualisation libraries, including Matplotlib, Plotly, Seaborn, Geopandas, and NetworkX. By mastering these libraries, you can transform raw data into compelling visual stories that aid in making informed decisions. 

Frequently Asked Questions

Upcoming Programming & DevOps Resources Batches & Dates

Date

building Python Course
Python Course

Thu 15th Aug 2024

Python Course

Thu 14th Nov 2024

Get A Quote

WHO WILL BE FUNDING THE COURSE?

cross

OUR BIGGEST SPRING SALE!

Special Discounts

red-starWHO WILL BE FUNDING THE COURSE?

close

close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.

close

close

Press esc to close

close close

Back to course information

Thank you for your enquiry!

One of our training experts will be in touch shortly to go overy your training requirements.

close close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.