laserson / squarify

Pure Python implementation of the squarify treemap layout algorithm
Other
293 stars 36 forks source link

Allow to specify text (label and/or value) location inside rectangles #32

Closed nmiranda closed 1 year ago

nmiranda commented 2 years ago

Hello team,

I found it useful to modify the plot function by adding a loc argument that allows the user to specify the desired location of text inside the rectangles. Until now this location was always the center of the rectangles.

For instance:

import squarify
import matplotlib.pyplot as plt

labels = ['A', 'B', 'C', 'D', 'E']
values = [500.0, 433.0, 78.0, 25.0, 25.0]

fig, axes = plt.subplots(1, 1)
ax = squarify.plot(sizes=values,
              pad=True,
              ax=axes,
              label=labels,
              value=values,
              loc="top left",
              )
ax.axis("off")

fig, axes = plt.subplots(1, 1)
ax = squarify.plot(sizes=values,
              pad=True,
              ax=axes,
              label=labels,
              value=values,
              loc="center",
              )
ax.axis("off")

fig, axes = plt.subplots(1, 1)
ax = squarify.plot(sizes=values,
              pad=True,
              ax=axes,
              label=labels,
              value=values,
              loc="bottom right",
              )
ax.axis("off")

plt.show()

Output: Figure_1 Figure_2 Figure_3

laserson commented 2 years ago

Thanks for the contribution @nmiranda! Could you add better user documentation to the valid possible loc strings and also perform error checking in case someone provides an invalid string? Also, please run the code through the black formatter.

nmiranda commented 2 years ago

-> Updated

nmiranda commented 2 years ago

-> Updated

laserson commented 1 year ago

Closing as stale