-1

我已经阅读了问题Matplotlib:避免在“散点图/点/蜂群”图中重叠数据点使用 matplotlib 将点的散点图添加到箱线图的问题,但是我想生成像这些用 R 生成的图:

R地块
(来源:eklund,网址:www.cbs.dtu.dk

是用于这些数字的代码。

我想用 matplotlib 来做,但到目前为止我只设法使用np.random.normal(i, 0.05). 这些点彼此分开,但我想订购它们。

这个答案与我想要的类似,但我的数据是非常接近但不同的浮点数,因此该groupby函数不起作用,我希望点对称于中心,如生成的图中所示R如上所示。

4

1 回答 1

1

正如这个问题Matplotlib的编辑中所指出的:避免在“散点图/点/蜂群”图中重叠数据点,而我一开始没有阅读,有一个用于这种图的 python 包:

https://github.com/mgymrek/pybeeswarm

而且这个包肯定比下面的代码做得更好。

我修改了这个答案的代码以接受浮点数,我得到的东西与我想要的有点相似。这是代码:

CA = [0,4,0,3,0,5]  
CB = [0,0,4,4,2,2,2,2,3,0,5] 
CC = [0.08423, 4.0078, 0.02936, 0.04862, 3.2105, 3.7796, 1.9974, 1.6986, 1.7443, 1.6615, 1, 1, 1]

lists = [CA, CB, CC]

x = []
y = []
for index1, my_list in enumerate(lists):
    scores_bins = {}
    for index2, score in enumerate(my_list):
        binx = round(score, 1)
        if binx not in scores_bins:
            scores_bins[binx] = []
        scores_bins[binx].append(score)

    for key, val in sorted(scores_bins.items()):
        values = scores_bins[key]
        points = len(values)
        pos = 1 + index1 + (1 - points) / 50.
        for value in values:
            x.append(pos)
            y.append(value)
            pos += 0.05

plt.plot(x, y, 'o')
plt.xlim((0,4))
plt.ylim((-1,6))

plt.show()

但是,如果 pos 增加,点会向右移动更多,而不仅仅是从中心向左右扩散......

于 2015-11-27T22:26:56.470 回答