Skip to content Skip to sidebar Skip to footer

Need To Visualize A Python Dictionary

I am working on a clustering algorithm which takes input from an excel file imported with pandas as a list. The list is divided into data blocks of like 8 floating points represent

Solution 1:

You could just do a scatter for each list of numbers as follows:

import matplotlib.pyplot as plt

cluster = {0: [0, 2, 4, 5, 6], 1: [1], 2: [3, 7]}
colours = ['green', 'orange', 'red']

fig = plt.figure()
ax = fig.add_subplot(111)

for colour, (x, ys) inzip(colours, cluster.items()):
    ax.scatter([x] * len(ys), ys, c=colour, linewidth=0, s=50)

plt.show()

Giving you:

multi coloured scatter plot

To extend this to use the colour map, colours could be constructed as:

colours = cm.rainbow(np.linspace(0, 1, len(cluster)))

e.g.

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np

cluster = {0: [0, 2, 4, 5, 6], 1: [1], 2: [3, 7]}
colours = cm.rainbow(np.linspace(0, 1, len(cluster)))

fig = plt.figure()
ax = fig.add_subplot(111)

for colour, (x, ys) inzip(colours, cluster.items()):
    ax.scatter([x] * len(ys), ys, c=colour, linewidth=0, s=50)

plt.show()

Post a Comment for "Need To Visualize A Python Dictionary"