Animations and facets with Plotly in Python

Data visualization is one of the highlights of data analysis as it involves a graphical representation of data. We can enhance our ability to interpret data findings greatly.

In this Answer, let's learn how to use Plotly's animations and facets today!

Plotly

Plotly is a data visualization library that allows us to create rich and dynamic visualizations in Python, R, and JavaScript. Some interesting features are:

  1. Zooming

  2. Tooltips

  3. Rotating

  4. Downloading plots

Animation

Animation in visualization is basically the display of frames, i.e., data points over time. This allows us to understand the data trends better.

Facet

Faceting is dividing a dataset into smaller subsets so that we can then display them in separate panels or grids. This helps in the comparison and analysis of different aspects of the data simultaneously.

Panels or grids of data
Panels or grids of data

Complete code

In our implementation, the animation is achieved using the animation_frame parameter in imshow(), allowing frames to be shown one after another. We establish facets using the facet_col parameter. This splits the data into columns to depict different aspects together.

Therefore, our goal of enhancing the understanding of changes over time within each aspect is achieved!

import plotly.express as px
from skimage import io

image_data = io.imread("https://github.com/scikit-image/skimage-tutorials/raw/main/images/cells.tif")

reshaped_data = image_data.reshape((15, 4, 256, 256))[5:]

fig = px.imshow(reshaped_data, animation_frame=0, facet_col=1, binary_string=True)

fig.update_layout(
    plot_bgcolor='black',
    paper_bgcolor='black',
    font_color='white',
)

fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[1]))

fig.show()
fig.write_html("output.html", auto_open=True)

Code explanation

  • Lines 1– 2: First and foremost, we import our needed libraries. We use plotly.express for creating interactive plots and the skimage library for image processing.

  • Line 4: Next, we use the imread() function from skimage to read the images from the specified link and store them in image_data.

  • Line 6: We use reshape() to modify the shape of the image and give the modification specifications as parameters.

    • 15 is the total number of animation frames present in the reshaped array.

    • 4 is the total channels per image. For instance, if the image is in RGB format, then its channels are red, blue, and green.

    • 256 is the height in pixels of each image.

    • 256 is the width in pixels of each image.

    • [5:] is the range on which array slicing is applied. In this case, the starting index is 5 and : represents that the end index is the last array index.

  • Line 8: We use the imshow() function and save the figure on the fig attribute. We pass the required figure specifications as a parameter.

    • reshaped_data is the reshaped image that we want to show.

    • animation_frame is set to 0 to specify that the initial animation frame is set to the first frame.

    • facet_col is set to 1 to specify the number of columns in which the data should be divided.

    • binary_string is set to True to represent the data in binary format.

  • Lines 10– 13: Now, we use update_layout() to customize the background color of the plot and paper and set the font color. We can customize our app according to our needs!

  • Line 16: We update the text content by using the for_each_annotation() function to iterate over each annotation and modify the value as per our requirements.

  • Line 18: Congratulations, we are now ready to show our interactive plot! We use show() to display the whole thing, including the image and histograms.

Note: To save the output in an HTML file, fig.write_html("output.html", auto_open=True) can be used.

Demonstration

This is how the animation of the cells is rendered within different facets.

Plotly allows zooming into the facets as well. Note that in this code, zooming in on one facet zooms in on each of the facets.

How well do you know animations and facets?

Question

How can we specify the number of columns within a plot in Plotly?

Show Answer

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved