简体   繁体   English

自动查找并添加坐标以在由不均匀列表字典制成的箱线图上添加注释(例如计数)

[英]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)

Output of my code我的代码的 Output

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.

相关问题 向箱线图添加图像注释 - Add image annotations to boxplot 如何使用任意嵌套对两个形状相同的列表执行逐元素算术运算(例如加、减、乘) - How to perform element-wise arithmetic operations (e.g. add, subtract, multiply) of two equally shaped lists with arbitrary nestings 在 Jinja2 中添加自定义令牌(例如 %% somevar %%) - Add custom tokens in Jinja2 (e.g. %% somevar %%) 如何 go 从 PANDASQL 中的特定日期向前/向后,例如 oracle date-1 和 impala DAYS_ADD(date,-1) - How to go forward/backward from a specific date in PANDASQL e.g. as in oracle date-1 ,and in impala DAYS_ADD(date,-1) 如何使用python和selenium从地图(例如Pokevision)上刮取GIS坐标? - How to scrape GIS coordinates from a map (e.g. Pokevision) using python and selenium? 如何在Python中添加不均匀的子列表? - How to add uneven sub-lists in Python? 添加/求和两个长度不相等的列表或元组 - Add/sum two lists or tuples of uneven length 从 Python 字典中打印特定键(例如,第三个键) - Print a specific key (e.g., third key) from a Python dictionary 将字典与不可散列或不可比较的值进行比较? (例如列表或数据框) - Compare Dictionaries with unhashable or uncomparable values? (e.g. Lists or Dataframes) 如何修改(例如添加/删除) ttk.Treeview 标签(tkinter)的值? - How to amend (e.g. add/remove) the values of a ttk.Treeview tag (tkinter)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM