Cool Data Visualizations in Python (code Included)

Featured image Cool Data Visualizations in Python (code included). #

Support my efforts and see my story at the bottom of the page.

In [23]:
# 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()

Heat Map, Spider Plot, Tree Diagram, using Avengers Data.

Data is completely random and in no way ment to serve as actual representation of Characters' traits.

In [24]:
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"])

# Plot the heatmap
heatmap_plot = sns.heatmap(data, center=0, cmap='gist_ncar')

plt.show()
In [10]:
# Import libs
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

# Get the data
df=pd.DataFrame(data)

data = {'Name':['Iron Man', 'Captain America', 'Thor', 'Hulk','Black Widow','Hawkeye'],
        'Attack':[83, 60, 80, 80, 52, 58],
        'Defense':[80, 62, 82, 100, 43 ,64],
        'Speed':[75, 63, 83, 67, 60, 58],
        'Range':[70, 80, 100, 44, 50, 80],
        'Health':[70,80,100,92,65,65],}

# Get the data for Iron Man
labels=np.array(["Attack","Defense","Speed","Range","Health"])
stats=df.loc[0,labels].values

# Make some calculations for the plot
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
stats=np.concatenate((stats,[stats[0]]))
angles=np.concatenate((angles,[angles[0]]))

# Plot stuff
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()
In [11]:
# Get the data for Captain America
labels=np.array(["Attack","Defense","Speed","Range","Health"])
stats=df.loc[1,labels].values

# Make some calculations for the plot
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
stats=np.concatenate((stats,[stats[0]]))
angles=np.concatenate((angles,[angles[0]]))

# Plot stuff
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[1,"Name"]])
ax.grid(True)

plt.show()
In [13]:
# Get the data for Thor
labels=np.array(["Attack","Defense","Speed","Range","Health"])
stats=df.loc[2,labels].values

# Make some calculations for the plot
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
stats=np.concatenate((stats,[stats[0]]))
angles=np.concatenate((angles,[angles[0]]))

# Plot stuff
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[2,"Name"]])
ax.grid(True)

plt.show()
In [14]:
# Get the data for Hulk
labels=np.array(["Attack","Defense","Speed","Range","Health"])
stats=df.loc[3,labels].values

# Make some calculations for the plot
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
stats=np.concatenate((stats,[stats[0]]))
angles=np.concatenate((angles,[angles[0]]))

# Plot stuff
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[3,"Name"]])
ax.grid(True)

plt.show()
In [15]:
# Get the data for Black Widow
labels=np.array(["Attack","Defense","Speed","Range","Health"])
stats=df.loc[4,labels].values

# Make some calculations for the plot
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
stats=np.concatenate((stats,[stats[0]]))
angles=np.concatenate((angles,[angles[0]]))

# Plot stuff
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[4,"Name"]])
ax.grid(True)

plt.show()
In [16]:
# Get the data for Hawkeye
labels=np.array(["Attack","Defense","Speed","Range","Health"])
stats=df.loc[5,labels].values

# Make some calculations for the plot
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
stats=np.concatenate((stats,[stats[0]]))
angles=np.concatenate((angles,[angles[0]]))

# Plot stuff
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[5,"Name"]])
ax.grid(True)

plt.show()

This is a tree disgram, it displays the Avengers with the most evenly matched stats.

In [22]:
# Import libs
import pandas as pd
from matplotlib import pyplot as plt
from scipy.cluster import hierarchy
import numpy as np


df = df.set_index('Name')
del df.index.name
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()

Credit: Seif, George. “4 More Quick and Easy Data Visualizations in Python with Code.” Medium, Towards Data Science, 4 May 2019, towardsdatascience.com/4-more-quick-and-easy-data-visualizations-in-python-with-code-da9030ab3429.