laserson / squarify

Pure Python implementation of the squarify treemap layout algorithm
Other
298 stars 37 forks source link

add plot method using matplotlib #1

Closed sinhrks closed 10 years ago

sinhrks commented 10 years ago

Added plot method using matplotlib. Matplotlib is imported in the plot method and it should not affect to dependencies.

Usage Example:

import squarify
import matplotlib.pyplot as plt
from numpy.random import rand

fig, axes = plt.subplots(2, 3, figsize=(14, 8))
plt.subplots_adjust(top=0.95, bottom=0.05, left=0.05, right=0.95, hspace=0.35)

sq = 8

def random_colors(n):
    return zip(rand(n), rand(n), rand(n))

labels = ['Sq{0}'.format(i) for i in range(sq)]

axes[0, 0].set_title('Default')
squarify.plot(rand(sq), ax=axes[0, 0])

axes[0, 1].set_title('Specify single color')
squarify.plot(rand(sq), color='r', ax=axes[0, 1])

axes[0, 2].set_title('Specify each colors')
squarify.plot(rand(sq), color=random_colors(sq), ax=axes[0, 2])

axes[1, 0].set_title('Specify labels')
squarify.plot(rand(sq), label=labels, ax=axes[1, 0])

sizes = rand(sq)
values = ['{0:0.2f}'.format(s) for s in sizes]
axes[1, 1].set_title('Specify values')
squarify.plot(sizes, value=values, ax=axes[1, 1])

axes[1, 2].set_title('Specify labels and values')
squarify.plot(sizes, label=labels, value=values, ax=axes[1, 2])
plt.show()

Output: squarify_plot

laserson commented 10 years ago

Thanks for the contribution!