Matplotlib - 图表上的注释重叠,如何将它们垂直均匀分布?

时间:2014-08-18 14:01:41

标签: python matplotlib annotations

我有一张图表,95%的置信区间作为补丁。当然,一些数据点重叠。因此,我需要动态分隔点标签,以便它们是人类可读的。我在下面有以下代码。如您所见,标签目前重叠。有任何建议如何让它们不重叠?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.transforms as transforms

from matplotlib.font_manager import FontProperties
from matplotlib.pyplot import *

mypath = ['1,0.025','1.01,0.05','1.02,0.035','1.03,0.040']

fig = plt.figure()
ax = fig.add_subplot(111)
Distances = []
Confidence_Intervals = []

for line in mypath:
    Distances.append(float(line.split(',')[0].strip()))
    Confidence_Intervals.append(float(line.split(',')[1].strip()))

ind = np.arange(len(Distances))
data = np.array(Distances)
y_error = np.array(Confidence_Intervals)
circles = []
plt.xlim(-1,1)
plt.ylim(0.8,1.1)

for a in range(len(ind)):
    ax.scatter(0, data[a], s=60, color='Black')
    trans = transforms.blended_transform_factory(ax.transData, ax.transData)
    circles.append(patches.Circle((0,data[a]),y_error[a], transform=trans, facecolor='yellow', alpha=0.5))

fig.set_size_inches(24,12)
for circle in circles:
    ax.add_patch(circle)
labels = ['{0}'.format(i) for i in range(len(data))]

for label, x, y in zip(labels, ind, data):
    plt.annotate(
        label, 
        xy = (0, y), xytext = (100, 0),
        textcoords = 'offset points', ha = 'right', va = 'bottom',
        bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
        arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))

plt.grid(True)
plt.legend(loc=0, scatterpoints = 1)
plt.ylabel('Pairwise distance (FastTree)')
plt.xlabel('Clade pairing')
plt.tick_params(axis='both', which='minor', labelsize=8)
plt.title('Sample Patch Chart')
axes().set_aspect('equal', 'datalim')
plt.show()

enter image description here

0 个答案:

没有答案