Animate your Python Graphs with Pillow | by Samir Saci | Aug, 2022
Create animated GIFs of your plots with the python library Pillow to bring life to your insights
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.
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.
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.
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,
- You import a batch of order lines with Order Number, Item Code, Location Coordinates
- You run the two algorithms that will create the routes for each order
- You export the results with the succession of locations to cover for each route
The results look like this,
- 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,
Intermediate Plot
If you truncate the lists used to plot the dotted lines, you can show the partial path
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,
- Generate plots of the partial paths: step by step
- Convert each plot to a picture using the library io and store it in a list
- 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
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,
- Build a loop that will generate all the frames of your gif
- Create a function to convert your plots to image
- 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
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
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.
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.
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.
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,
- You import a batch of order lines with Order Number, Item Code, Location Coordinates
- You run the two algorithms that will create the routes for each order
- You export the results with the succession of locations to cover for each route
The results look like this,
- 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,
Intermediate Plot
If you truncate the lists used to plot the dotted lines, you can show the partial path
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,
- Generate plots of the partial paths: step by step
- Convert each plot to a picture using the library io and store it in a list
- 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
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,
- Build a loop that will generate all the frames of your gif
- Create a function to convert your plots to image
- 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
If you are interested in Supply Chain Analytics, have a look at my website