我正在尝试制作散点图并使用列表中的不同数字注释数据点.所以例如我想绘制y vs x并使用n中的相应数字进行注释.
y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]
ax = fig.add_subplot(111)
ax1.scatter(z, y, fmt='o')
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我试图在我的图表中停止注释文本重叠.在接受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制作一个散点图:
plt.scatter(randn(100),randn(100))
# set x, y lims
plt.xlim([...])
plt.ylim([...])
Run Code Online (Sandbox Code Playgroud)
我想(x, y)用箭头指向它和标签来注释给定点.我知道这可以完成annotate,但我希望箭头及其标签以"最佳"方式放置,如果可能(给定当前轴比例/限制)箭头和标签不重叠与其他点.例如,如果你想标记异常点.有没有办法做到这一点?它不一定是完美的,而只是箭头/标签的智能放置,只给出(x,y)要标记的点的坐标.谢谢.
我最近看到这个包用于R/ggplot2,它允许一个人在一个绘图上有多个注释并自动调整它们的位置以最小化重叠,这样就提高了可读性.python/matplotlib有类似的东西吗?
编辑:我发现Matplotlib重叠注释/文本,它看起来很有希望,但似乎结果不如R包.
例:
from matplotlib import pyplot as plt
import numpy as np
xs = np.arange(10, step=0.1)+np.random.random(100)*3
ys = np.arange(10, step=0.1)+np.random.random(100)*3
labels = np.arange(100)
plt.scatter(xs, ys)
for x, y, s in zip(xs, ys, labels):
plt.text(x, y, s)
plt.show()
Run Code Online (Sandbox Code Playgroud)
你可以看到,即使这样的短标签在数据密度很高时也会造成一个疯狂的混乱.
我有一个带有许多点的散点图.每个点都有一个与之相关的字符串(长度不一样),我想提供一个标签,但我不能适应它们.所以我想迭代我的数据点从最重要到最不重要,并且在每种情况下仅在标签不与现有标签重叠时才应用标签.琴弦长度不一.其中一位评论者提到解决背包问题以找到最佳解决方案.在我的情况下,贪心算法(总是标记可以被标记而没有重叠的最重要的剩余点)将是一个良好的开端并且可能就足够了.
这是一个玩具的例子.我可以让Python只标记尽可能多的点而不重叠吗?
import matplotlib.pylab as plt, numpy as np
npoints = 100
xs = np.random.rand(npoints)
ys = np.random.rand(npoints)
plt.scatter(xs, ys)
labels = iter(dir(np))
for x, y, in zip(xs, ys):
# Ideally I'd condition the next line on whether or not the new label would overlap with an existing one
plt.annotate(labels.next(), xy = (x, y))
plt.show()
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,这使得“气泡”在某些位置变得混乱,因为它们重叠而难以阅读。有什么办法可以“自动放置”,使它们不重叠。例如一些“气泡”以不重叠的方式位于情节线的上方和下方。
有谁知道是否有可能在matplotlib中生成这样的图?图像取自以下页面.如果没有,还有另一个'python'解决方案来生成这样的情节?

我正在使用基本axis.annotate(str(i))函数来显示沿图形点的值。问题是它们很快就会聚集在一起。所以我有两个问题:如何删除注释?我怎样才能缩小一个(字体大小)?
这里是对注释方法的引用:http : //matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.annotate
我已经完成了我的研究,但令人惊讶的是什么也没找到。干杯。
matplotlib ×8
python ×8
annotate ×2
plot ×2
scatter-plot ×2
annotations ×1
numpy ×1
pandas ×1
python-3.x ×1
scipy ×1
text ×1