Skip to content
Advertisement

Adding legend to heatmap in Python / Matplotlib with circles that compare total users, and colors that indicate ratio of abandonment in single graph

I found in SO an explanation to add a legend to this type of graph. However, it does not translate well with my data. I got my graph with circles and shades of colors to work. I do have the legend indicating what the shades of color mean in terms of ratio of users abandoning a chatbot at a given question (node). The size of the circles indicate number of users passing thru a particular node. I would like to have a legend to give an idea of the size of the node. Furthermore, I would like to add to each circle the percentage of people abandoning, i.e., what the color shading refers to. An even better solution would be to have two values per circle, i.e. the percentage of abandonment as indicated before + the total number of users per node. Is this possible?

To summarize:

  • Can I add a legend to indicate what approximately the size of each circle represents with respect to the number of users at a given node. For example, the legend could have three circles, a small circle which represents 50 users, a medium circle that represents 500 users and a large circle that represents 1500 users.
  • Can I add an annotation for both variables on each circle, i.e. ratio of abandonment and total users per node, placed on top of each circle. If only one annotation is posible, then the ratio would be the most important between the two.

This was somewhat answered in another post, but it only worked for the data that person used, and this was actually mentioned in the solution, where getting the legend to work for a different set of data was not resolved. Also, there is very little information about these types of graphs elsewhere in the internet, thus, I hope you can help me.

Here is how the graph looks now: enter image description here

Below is the code that generates the graph without the annotations and without the second legend for the size of the circles.

JavaScript

Advertisement

Answer

You wish to add a legend for the circles you have added to the heatmap. Next, I have created the code with the understanding that you want to add an annotation to that circle. regarding the first, adding the legend, the easiest way is to use a scatterplot and use the number of users for the color and size. By sizing the elements that make up the legend for that scatter plot, the legend is automatically created. The number of this legend can be an integer or ‘auto’. We have specified four as the number of legends that is closest to your preference. As for the annotations, they are added in a loop process from the data frame, with a simple formatting function. If you need to position or decorate the annotations, you can use ax.annotate().

JavaScript

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement