Four Advanced Data Visualization Techniques in Python: Heat Map, 2D Density Plot, Spider Plot, and Tree Diagram
This article introduces four advanced Python data‑visualization methods—heat map, 2D density plot, spider (radar) plot, and hierarchical tree diagram—explaining their concepts, practical use cases, and providing complete seaborn, matplotlib, and SciPy code examples for each.
Data visualization is a crucial step in data science and machine learning projects, helping both exploratory analysis and final presentation for non‑technical audiences.
Heat Map – A matrix‑style visual where each cell’s value is encoded by color, useful for revealing relationships among multiple features.
# Importing libs
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create a random dataset
data = pd.DataFrame(np.random.random((10,6)), columns=["Iron Man","Captain America","Black Widow","Thor","Hulk", "Hawkeye"])
print(data)
# Plot the heatmap
heatmap_plot = sns.heatmap(data, center=0, cmap='gist_ncar')
plt.show()2D Density Plot – Extends the 1‑D density plot to two variables, showing joint probability distribution with color shading.
# Importing libs
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.stats import skewnorm
# Create the data
speed = skewnorm.rvs(4, size=50)
size = skewnorm.rvs(4, size=50)
# Create and show the 2D Density plot
ax = sns.kdeplot(speed, size, cmap="Reds", shade=False, bw=.15, cbar=True)
ax.set(xlabel='speed', ylabel='size')
plt.show()Spider Plot – Also known as a radar chart, it visualizes multiple variables for a single entity, making it easy to compare attributes such as the stats of Avengers characters.
# Import libs
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
# Get the data
df = pd.read_csv("avengers_data.csv")
print(df)
"""
# Name Attack Defense Speed Range Health
0 1 Iron Man 83 80 75 70 70
1 2 Captain America 60 62 63 80 80
2 3 Thor 80 82 83 100 100
3 3 Hulk 80 100 67 44 92
4 4 Black Widow 52 43 60 50 65
5 5 Hawkeye 58 64 58 80 65
"""
# Get the data for Iron Man
labels = np.array(["Attack","Defense","Speed","Range","Health"])
stats = df.loc[0, labels].values
# Prepare angles for radar chart
angles = np.linspace(0, 2*np.pi, len(labels), endpoint=False)
stats = np.concatenate((stats, [stats[0]]))
angles = np.concatenate((angles, [angles[0]]))
# Plot radar chart
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.plot(angles, stats, 'o-', linewidth=2)
ax.fill(angles, stats, alpha=0.25)
ax.set_thetagrids(angles * 180/np.pi, labels)
ax.set_title([df.loc[0, "Name"]])
ax.grid(True)
plt.show()Tree Diagram – A hierarchical clustering visualization built with SciPy’s Ward linkage, useful for showing similarity relationships, illustrated here with a subset of a Pokémon dataset.
# Import libs
import pandas as pd
import matplotlib.pyplot as plt
from scipy.cluster import hierarchy
import numpy as np
# Read in the dataset
# Drop any fields that are strings
# Only get the first 40 because this dataset is big
df = pd.read_csv('Pokemon.csv')
df = df.set_index('Name')
del df.index.name
df = df.drop(["Type 1","Type 2","Legendary"], axis=1)
df = df.head(n=40)
# Calculate the distance between each sample
Z = hierarchy.linkage(df, 'ward')
# Orientation our tree
hierarchy.dendrogram(Z, orientation="left", labels=df.index)
plt.show()The article concludes with a QR code that offers free access to a Python public course and a large collection of learning resources.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.