Heatmap, 2D Density Plot, Spider Plot, and Dendrogram Visualization with Python
This article introduces four Python visualization techniques—heatmaps, 2D density plots, spider (radar) plots, and dendrograms—explaining their concepts, typical use cases, and providing complete code examples using seaborn, matplotlib, and scipy to help readers quickly create insightful charts.
Heatmaps represent data as a matrix where each cell’s value is shown by a color, making it easy to compare multiple feature variables at a glance. They are especially useful for visualizing relationships between two dimensions of a dataset.
Example code using seaborn to generate a random dataset and plot a heatmap:
<code>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()</code>2D Density Plots extend the concept of a 1‑D density plot to two variables, showing the joint probability distribution with color‑coded contours. They are ideal for identifying regions where data points concentrate in a two‑dimensional space.
Example code creating a skewed distribution and visualizing it with seaborn.kdeplot :
<code>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()</code>Spider (Radar) Plots display multiple variables for a single entity on axes radiating from a central point, allowing quick visual comparison of strengths and weaknesses across categories. The article demonstrates plotting Avengers characters’ attributes using matplotlib in polar coordinates.
<code>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")
labels=np.array(["Attack","Defense","Speed","Range","Health"])
stats=df.loc[0,labels].values
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
stats=np.concatenate((stats,[stats[0]]))
angles=np.concatenate((angles,[angles[0]]))
# Plot
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()</code>Dendrograms (tree diagrams) illustrate hierarchical relationships between samples. By computing pairwise distances and applying a linkage method, one can visualize clusters; the article uses scipy.cluster.hierarchy to plot a dendrogram of a subset of Pokémon statistics.
<code>import pandas as pd
from matplotlib import pyplot as plt
from scipy.cluster import hierarchy
import numpy as np
# Read dataset and preprocess
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)
# Compute linkage and plot dendrogram
Z = hierarchy.linkage(df, 'ward')
hierarchy.dendrogram(Z, orientation="left", labels=df.index)
plt.show()</code>Overall, the article provides clear explanations of each visualization type, discusses when to use them, and supplies ready‑to‑run Python snippets so readers can reproduce the charts and adapt them to their own data.
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.