我试图在我的图表中停止注释文本重叠.在接受Matplotlib重叠注释的答案中建议的方法看起来非常有希望,但是对于条形图而言.我无法将"轴"方法转换为我想要做的事情,而且我不明白文本是如何排列的.
import sys
import matplotlib.pyplot as plt
# start new plot
plt.clf()
plt.xlabel("Proportional Euclidean Distance")
plt.ylabel("Percentage Timewindows Attended")
plt.title("Test plot")
together = [(0, 1.0, 0.4), (25, 1.0127692669427917, 0.41), (50, 1.016404709797609, 0.41), (75, 1.1043426359673716, 0.42), (100, 1.1610446924342996, 0.44), (125, 1.1685687930691457, 0.43), (150, 1.3486407784550272, 0.45), (250, 1.4013999168008104, 0.45)]
together.sort()
for x,y,z in together:
plt.annotate(str(x), xy=(y, z), size=8)
eucs = [y for (x,y,z) in together]
covers = [z for (x,y,z) in together]
p1 = plt.plot(eucs,covers,color="black", alpha=0.5)
plt.savefig("test.png")
Run Code Online (Sandbox Code Playgroud)
图像(如果有效)可以在这里找到(此代码):

而 …
是否可以确定matplotlib文本对象的尺寸?如何找到像素的宽度和高度?
谢谢
编辑:我想我找到了一种方法来做到这一点.我在下面列举了一个例子.
import matplotlib as plt
f = plt.figure()
r = f.canvas.get_renderer()
t = plt.text(0.5, 0.5, 'test')
bb = t.get_window_extent(renderer=r)
width = bb.width
height = bb.height
Run Code Online (Sandbox Code Playgroud) 我有以下代码片段:
data.plot(y='Close', ax = ax)
newdates = exceptthursday.loc[start:end]
for anotate in (newdates.index + BDay()).strftime('%Y-%m-%d'):
ax.annotate('holliday', xy=(anotate, data['Close'].loc[anotate]), xycoords='data',
xytext=(-30, 40), textcoords='offset points',
size=13, ha='center', va="baseline",
bbox=dict(boxstyle="round", alpha=0.1),
arrowprops=dict(arrowstyle="wedge,tail_width=0.5", alpha=0.1));
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我已经明确提到了xytext,这使得“气泡”在某些位置变得混乱,因为它们重叠而难以阅读。有什么办法可以“自动放置”,使它们不重叠。例如一些“气泡”以不重叠的方式位于情节线的上方和下方。
我想制作一个带有几十个点的散点图,这些点可能彼此非常接近.我从问题的答案中尝试了注释方法:
但是当点彼此足够接近时,您可以看到标签/注释重叠.是否有任何库或方法可以生成这样的图,其中各个注释不会相互冲突,也不会与图的边界发生冲突?
import matplotlib.pyplot as plt
z = [0.15, 0.3, 0.45, 0.46, 0.6, 0.75]
y = [2.56422, 3.77284, 3.52623, 3.52623, 3.51468, 3.02199]
n = [58, 651, 393, "393(2)", 203, 123]
fig, ax = plt.subplots()
ax.scatter( z, y )
for i, txt in enumerate( n ):
ax.annotate( txt, ( z[i] + .01, y[i] + .01 ) )
plt.show()Run Code Online (Sandbox Code Playgroud)
matplotlib ×4
python ×4
annotate ×1
annotations ×1
label ×1
pandas ×1
plot ×1
python-3.x ×1
scatter-plot ×1