Techno Blender
Digitally Yours.

Animate your Python Graphs with Pillow | by Samir Saci | Aug, 2022

0 247


Create animated GIFs of your plots with the python library Pillow to bring life to your insights

Photo by Awesome Sauce Creative on Unsplash

Scenario
You are a data scientist that has developed an algorithm or performed some advanced analysis linked to a dynamic process.

Because you want to show the impact of your solution, you plot the results using a conventional library of python.

Example of a Pathfinding Solution — (Image by Author)

However, your graphs are not self-explicit and do not reflect the dynamic aspect of the process.

Objective
In this article, we will use this example of pathfinding algorithms to show how you can bring additional insights by building GIF animations with Python Pillow.

Problem Statement

In a Warehouse, walking time from one location to another during the picking route can account for 60% to 70% of the operator’s working time.

Reducing this walking time is the most effective way to increase your overall productivity.

Example of routes with three locations — (Image by Author)

Therefore, you have developed several algorithms that optimize the route of warehouse operators to reduce their walking distance.

Task

Using an example of a long-picking mission, you want to use a graph to show the difference between the two algorithms.

Graph with the two solutions — (Image by Author)

Each dot is a picking location and the numbers are representing the order in the picking route of the operator.

However, these graphs are not self-explicit and we miss the different steps that lead to this final result.

Objective

Let us animate this graph to see the sequence location by location to understand the difference between the two solutions.

Process

Let me first explain how to plot the graph presented above,

  1. You import a batch of order lines with Order Number, Item Code, Location Coordinates
  2. You run the two algorithms that will create the routes for each order
  3. You export the results with the succession of locations to cover for each route
Picking Root Optimization — (Image by Author)

The results look like this,

Dataframe df with the results — (Data by Author)
  • In Path OR and Path Init you have the succession of 2D coordinates
  • In Distance OR and Distance Init you have the total distance

Plot

Let’s take the line with the biggest gap in distance between the two methods and plot the results.

Import the libraries

Because we are going to use scatter plots, we need to extract the coordinates by location for the two solutions

  • We create two lists of the location coordinates for the two solutions
  • For each list, we create two sublists to take the x-axis and y-axis coordinates

We can use now the lists (x1, y1, x2, y2) to plot the two paths,

  • Plot a dot for each location coordinate (xi, yi) with xi in x1 and yi in y1
  • Link the dots with dotted lines ‘ — ‘
  • Annotate each dot with the order of the location in the path

The final result is a plot of the complete paths,

Graph with the two solutions — (Image by Author)

Intermediate Plot

If you truncate the lists used to plot the dotted lines, you can show the partial path

Three first locations — (Image by Author)

The objective will be to generate a GIF of these partial paths to show the succession of locations for the two solutions.

The process will be simple,

  1. Generate plots of the partial paths: step by step
  2. Convert each plot to a picture using the library io and store it in a list
  3. Create a gif using PIL with the generated list

Step 1: Create the function to convert to a picture

This function will take the figures generated with matplotlib and return a picture with the right format for PIL.

Step 2: Build the loop to plot each step

The images are stored in the list: list_gif

Step 3: Create the Gif

You can create the gif with one line of code,

  • You append the first image with the rest of the list
  • You can select the duration per picture in (ms)
  • Select infinite loop with the option: loop = 0

Final Result

Final Results — (Image by Author)

You can now see the succession of picking locations and understand where the two algorithms differ.

With this straightforward example, you can understand the different steps to follow to generate your animated graphs,

  1. Build a loop that will generate all the frames of your gif
  2. Create a function to convert your plots to image
  3. Build a gif with all the frames choosing the speed and the number of loops

In my previous articles, you can find other applications of this method

Transportation Route — (Image by Author)

If you are interested in Supply Chain Analytics, have a look at my website


Create animated GIFs of your plots with the python library Pillow to bring life to your insights

Photo by Awesome Sauce Creative on Unsplash

Scenario
You are a data scientist that has developed an algorithm or performed some advanced analysis linked to a dynamic process.

Because you want to show the impact of your solution, you plot the results using a conventional library of python.

Example of a Pathfinding Solution — (Image by Author)

However, your graphs are not self-explicit and do not reflect the dynamic aspect of the process.

Objective
In this article, we will use this example of pathfinding algorithms to show how you can bring additional insights by building GIF animations with Python Pillow.

Problem Statement

In a Warehouse, walking time from one location to another during the picking route can account for 60% to 70% of the operator’s working time.

Reducing this walking time is the most effective way to increase your overall productivity.

Example of routes with three locations — (Image by Author)

Therefore, you have developed several algorithms that optimize the route of warehouse operators to reduce their walking distance.

Task

Using an example of a long-picking mission, you want to use a graph to show the difference between the two algorithms.

Graph with the two solutions — (Image by Author)

Each dot is a picking location and the numbers are representing the order in the picking route of the operator.

However, these graphs are not self-explicit and we miss the different steps that lead to this final result.

Objective

Let us animate this graph to see the sequence location by location to understand the difference between the two solutions.

Process

Let me first explain how to plot the graph presented above,

  1. You import a batch of order lines with Order Number, Item Code, Location Coordinates
  2. You run the two algorithms that will create the routes for each order
  3. You export the results with the succession of locations to cover for each route
Picking Root Optimization — (Image by Author)

The results look like this,

Dataframe df with the results — (Data by Author)
  • In Path OR and Path Init you have the succession of 2D coordinates
  • In Distance OR and Distance Init you have the total distance

Plot

Let’s take the line with the biggest gap in distance between the two methods and plot the results.

Import the libraries

Because we are going to use scatter plots, we need to extract the coordinates by location for the two solutions

  • We create two lists of the location coordinates for the two solutions
  • For each list, we create two sublists to take the x-axis and y-axis coordinates

We can use now the lists (x1, y1, x2, y2) to plot the two paths,

  • Plot a dot for each location coordinate (xi, yi) with xi in x1 and yi in y1
  • Link the dots with dotted lines ‘ — ‘
  • Annotate each dot with the order of the location in the path

The final result is a plot of the complete paths,

Graph with the two solutions — (Image by Author)

Intermediate Plot

If you truncate the lists used to plot the dotted lines, you can show the partial path

Three first locations — (Image by Author)

The objective will be to generate a GIF of these partial paths to show the succession of locations for the two solutions.

The process will be simple,

  1. Generate plots of the partial paths: step by step
  2. Convert each plot to a picture using the library io and store it in a list
  3. Create a gif using PIL with the generated list

Step 1: Create the function to convert to a picture

This function will take the figures generated with matplotlib and return a picture with the right format for PIL.

Step 2: Build the loop to plot each step

The images are stored in the list: list_gif

Step 3: Create the Gif

You can create the gif with one line of code,

  • You append the first image with the rest of the list
  • You can select the duration per picture in (ms)
  • Select infinite loop with the option: loop = 0

Final Result

Final Results — (Image by Author)

You can now see the succession of picking locations and understand where the two algorithms differ.

With this straightforward example, you can understand the different steps to follow to generate your animated graphs,

  1. Build a loop that will generate all the frames of your gif
  2. Create a function to convert your plots to image
  3. Build a gif with all the frames choosing the speed and the number of loops

In my previous articles, you can find other applications of this method

Transportation Route — (Image by Author)

If you are interested in Supply Chain Analytics, have a look at my website

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment