New to Plotly?¶
Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!
Plotly's python API is updated frequently. To upgrade, run
pip install plotly --upgrade.
Define the coordinate system for the returned rectangles: these values will range from x to x + width and y to y + height.
Then define your treemap values. The sum of the treemap values must equal the total area to be laid out (i.e. width
* height). The values must be sorted in descending order and must be positive.
import plotly.plotly as py import plotly.graph_objs as go import squarify x = 0. y = 0. width = 100. height = 100. values = [500, 433, 78, 25, 25, 7] normed = squarify.normalize_sizes(values, width, height) rects = squarify.squarify(normed, x, y, width, height) # Choose colors from http://colorbrewer2.org/ under "Export" color_brewer = ['rgb(166,206,227)','rgb(31,120,180)','rgb(178,223,138)', 'rgb(51,160,44)','rgb(251,154,153)','rgb(227,26,28)'] shapes =  annotations =  counter = 0 for r in rects: shapes.append( dict( type = 'rect', x0 = r['x'], y0 = r['y'], x1 = r['x']+r['dx'], y1 = r['y']+r['dy'], line = dict( width = 2 ), fillcolor = color_brewer[counter] ) ) annotations.append( dict( x = r['x']+(r['dx']/2), y = r['y']+(r['dy']/2), text = values[counter], showarrow = False ) ) counter = counter + 1 if counter >= len(color_brewer): counter = 0 # For hover text trace0 = go.Scatter( x = [ r['x']+(r['dx']/2) for r in rects ], y = [ r['y']+(r['dy']/2) for r in rects ], text = [ str(v) for v in values ], mode = 'text', ) layout = dict( height=700, width=700, xaxis=dict(showgrid=False,zeroline=False), yaxis=dict(showgrid=False,zeroline=False), shapes=shapes, annotations=annotations, hovermode='closest' ) # With hovertext figure = dict(data=[trace0], layout=layout) # Without hovertext # figure = dict(data=[Scatter()], layout=layout) py.iplot(figure, filename='squarify-treemap')