#201 Control the color of Treemap

 

 

 

 

 

This page aims to describe the customization you can apply on a treemap. You can easily change the color of each group using the following code:

 

 

 

 

 

#libraries
import matplotlib.pyplot as plt
import squarify # pip install squarify (algorithm for treemap)

# Change color
squarify.plot(sizes=[13,22,35,5], label=["group A", "group B", "group C", "group D"], color=["red","green","blue", "grey"], alpha=.4 )
plt.axis('off')
plt.show()
  • Sponsors

  • 2 comments

    • Hi,
      I’m using colorgram to analyse an image and want to use this data to show a tree diagram of the top 10 colors extracted.

      My code all works but the diagram only shows a weak pastel version of the rgbs that are passed to it, any ideas how I can get it to show the actual colour?

      Here’s my code (sorry it’s a bit messy):

      import colorgram
      import matplotlib.pyplot as plt
      import squarify

      col = colorgram.extract(‘filelocation.jpg’, 10)
      torep = (‘Rgb’, ‘r=’, ‘b=’, ‘g=’, ‘ ‘, ‘(‘,’)’)
      squares = []
      labels = []
      colors = []

      for c in col:
      rgbstr = str(c.rgb)
      for rep in torep:
      rgbstr = rgbstr.replace(rep, ”)
      rgbstr = rgbstr.split(‘,’)
      rgb = [float(x) / 255 for x in rgbstr]
      squares.append(c.proportion)
      colors.append(rgb)

      squarify.plot(sizes=squares, color=colors, alpha=.4)
      plt.axis(‘off’)
      plt.show()

      Reply

    Leave a Reply

    Your email address will not be published.