Search code examples
pythondictionaryannotationsboxplot

Automatically find and add the coordinates to add Annotations (e.g. count) on a Boxplot made from a Dictionary of uneven Lists


I'm pretty new in programming world and I'm really frustrated to solve a problem which I thought should be really easy...

Case: Let's say I have a Dictionary with uneven Lists; Also the number of Keys(string) & Values(number) could change anytime.

Need: I want to annotate (add text or whatever) some Information (e.g. count) to each Subplots or Categories (each Key is an individual Category).

Problem: I found many solutions for evenly numbered Categories, which apparently doesn't work for me. e.g. Solution

I also found some Answers e.g. Solution , that I should first get the Coordinates of each Keys in the x-line and then do a inverted transformation to work with the "log scales". Which was so far the best solution for me, but unfortunately it does not really fit the Coordinates and I couldn't get & add the points automatically before using plt.show().

I could also guess the coordinates with trial error in the Transformation Method or with Offset e.g. Solution. But as I said, my Dictionary could change anytime, and then I should do it again every time!

I think there should be much more simpler method to solve this problem, but I couldn't find it.

Here is the simplified example of my Code and what I tried:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import (TextArea, DrawingArea, OffsetImage,
                                  AnnotationBbox)

dictionary = {}
dictionary["a"] = [1, 2, 3, 4, 5]
dictionary["b"] = [1, 2, 3, 4, 5, 6, 7]

fig, ax = plt.subplots()
ax.boxplot(dictionary.values())
x = ax.set_xticklabels(dictionary.keys())

fig.text(x = 0.25, y = 0, s = str(len(dictionary["a"])))
fig.text(x = 0.75, y = 0, s = str(len(dictionary["b"])))


plt.show()


crd = np.vstack((ax.get_xticks(), np.zeros_like(ax.get_xticks()))).T
ticks = ax.transAxes.inverted().transform(ax.transData.transform(crd))

print(ticks[:,0])



# ab = AnnotationBbox(TextArea("text"), xy=(1, 0), xybox =(0, -30), boxcoords="offset points",pad=0,frameon=False )
# ax.add_artist(ab)

Output of my code


Solution

  • as i understand you may want something like this:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.offsetbox import (TextArea, DrawingArea, OffsetImage,
                                      AnnotationBbox)
    
    dictionary = {}
    dictionary["a"] = [1, 2, 3, 4, 5]
    dictionary["b"] = [1, 2, 3, 4, 5, 6, 7]
    dictionary["cex"] = [1, 2, 3]
    
    fig, ax = plt.subplots()
    ax.boxplot(dictionary.values())
    x = ax.set_xticklabels(dictionary.keys())
    
    ticksList=ax.get_xticks()
    print (ticksList)
    for x in ticksList:
        ax.text(x, 0,str(len(list(dictionary.values())[x-1])),fontdict={'horizontalalignment': 'center'})
    
    fig.show()