plotly

plotly.express.icicle

An icicle plot or icicle chart is used to represent hierarchical data using rectangular sectors that cascade from the root note. You can then navigate the data through various sections. They are closely similar to Treemaps plots allowing you to define the hierarchy via labels.

In this article, we will explore how to create icicle plots using the Plotly Express module.

Plotly.express.icicle()

To create an icicle plot with plotly, we use the icicle() function. The function syntax is as shown:

plotly.express.icicle(data_frame=None, names=None, values=None, parents=None, path=None, ids=None, color=None, color_continuous_scale=None, range_color=None, color_continuous_midpoint=None, color_discrete_sequence=None, color_discrete_map=None, hover_name=None, hover_data=None, custom_data=None, labels=None, title=None, template=None, width=None, height=None, branchvalues=None, maxdepth=None)

The following are the most common parameters you will need to use when working with the icicle plots:

  1. Data_frame – specifies data frame whose values are used as column names for the plot.
  2. Names – specifies the values used as labels for the sectors. You can pass this as column name within a data frame or a Pandas Series.
  3. Values – specifies the values used to set the values associated with the sectors. Similar to the names parameter, you can pass this value as a column name or a Pandas Series.
  4. Parents – specifies the values used as the parents.
  5. Path – defines the values used for defining the hierarchy of the sectors.
  6. Ids – sets the values used to set the ids of the sectors. You can pass this value either as a column within a DataFrame or a Pandas Series.
  7. Color – defines the values used to assign a unique color to the marks of the plot.
  8. Title – sets the title for the figure.
  9. Width/height – sets the width and height of the figure in pixels.

The function will return the icicle chart as graph_objects.Figure type.

Basic icicle Plot

To illustrate how to create an icicle chart using Plotly Express, we can run a sample code as shown:

import plotly.express as px

df = dict(
    names = ['Oleh', 'Jovian', 'Galatea', 'Herman', 'Kublai', 'Yvette', 'Stina', 'Andrej'],
    parents=['Yvette', 'Yvette', 'Yvette', 'Yvette', 'Yvette', 'Andrej', 'Andrej', ''],
)
fig = px.icicle(df, names='names', parents='parents')
fig.show()

The code above should return a figure as shown:

Specifying Path

We can represent hierarchical data with each column representing a different level of hierarchy. We can use the path parameter to represent to path for the data as shown:

df = px.data.tips()
fig = px.icicle(df, path=[px.Constant('all'), 'day', 'time', 'sex'], values='total_bill')
fig.show()

The resulting figure:

Set Color

We can also specify the column which we wish to use as the color for the sectors. Example:

df = px.data.tips()
fig = px.icicle(df, path=[px.Constant('all'), 'day', 'time', 'sex'], values='total_bill', color='day')
fig.show()

Figure:

And that’s it for this one.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list