[英]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 (eg 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.问题:我找到了许多偶数类别的解决方案,这显然对我不起作用。 eg Solution 例如解决方案
I also found some Answers eg 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".我还找到了一些答案, 例如解决方案,我应该首先获取 x 线中每个键的坐标,然后进行反向转换以使用“对数刻度”。 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().这对我来说是迄今为止最好的解决方案,但不幸的是它并不真正适合坐标,我无法在使用 plt.show() 之前自动获取和添加点。
I could also guess the coordinates with trial error in the Transformation Method or with Offset eg Solution .我还可以通过 Transformation Method 中的试错或 Offset 例如 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)
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.