我正在尝试标记多个最大值但无法正确显示它们。此外,标签距离与图例重叠的点太远。
import matplotlib.pyplot as plt
import numpy as np
x1,y1= np.loadtxt('MaxMin1.txt', dtype=str, unpack=True)
x1 = x1.astype(int)
y1 = y1.astype(float)
x2,y2= np.loadtxt('MaxMin2.txt', dtype=str, unpack=True)
x2 = x2.astype(int)
y2 = y2.astype(float)
x3,y3= np.loadtxt('MaxMin3.txt', dtype=str, unpack=True)
x3 = x3.astype(int)
y3 = y3.astype(float)
#-------------------------------------------------------------
def annot_max(x,y, ax=None):
xmax = x[np.argmax(y)]
ymax = y.max()
text= "x={:.3f}, y={:.3f}".format(xmax, ymax)
if not ax:
ax=plt.gca()
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
arrowprops=dict(arrowstyle="->",connectionstyle="angle,angleA=0,angleB=60")
kw = dict(xycoords='data',textcoords="axes fraction",
arrowprops=arrowprops, bbox=bbox_props, ha="left", va="top")
ax.annotate(text, xy=(xmax, ymax), xytext=(0.94,0.96), **kw)
#-------------------------------------------------------------
fig=plt.figure()
fig.show()
ax=fig.add_subplot(111)
ax.plot(x1,y1,c='b',ls='-',label='Recovery',fillstyle='none')
ax.plot(x2,y2,c='g',ls='-',label='Normal')
ax.plot(x3,y3,c='r',ls='-',label='No-Recovery')
annot_max(x1,y1)
annot_max(x2,y2)
annot_max(x3,y3)
plt.legend(loc=1)
# naming the x axis
plt.xlabel('<------Instances(count)------>')
# naming the y axis
plt.ylabel('Acceleration (m/sq.sec)')
# giving a title to my graph
plt.title('Fall Detection Comparison graph')
plt.show()
我得到的输出是Output Graph 我刚开始使用 python,所以我可能无法理解非常小的提示。请帮忙。
最佳答案
您需要更改函数 annot_max
。
首先,根据 xytext=(0.94,0.96)
,所有标签都出现在同一个位置。您必须根据 xmax 和 ymax 的坐标指定标签位置,如下所示。
然后将 textcoords
更改为 data
值以根据数据而不是轴分数来操作标签位置。在我的示例中,xytext=(xmax+.5,ymax+5)
表示标签框的位置将从 xmax 偏移 +.5 点,从 ymax 偏移 +5 点(您必须使用自己的值根据您的数据)。
但我建议您手动放置标签,因为您最多可以放置 3 个标签(在参数 xytext
中指定每个标签框的位置,例如 xytext=(100,40)
-你的第一个最大值)。
Matplotlib 无法自动避免文本框重叠。
要挤压标签框,您可以将文本放在两行中,即 text= "x={:.3f},\ny={:.3f}".format(xmax, ymax)
.
import matplotlib.pyplot as plt
import numpy as np
x1=np.array([1,2,3,4,5,6,7,8,9,10])
x2 = x1[:]
x3 = x1[:]
y1=np.array([1,2,3,100,5,6,7,8,9,10])
y2=np.array([50,2,3,4,5,6,7,8,9,10])
y3=np.array([1,2,3,4,5,6,75,8,9,10])
#-------------------------------------------------------------
def annot_max(x,y, ax=None):
xmax = x[np.argmax(y)]
ymax = y.max()
text= "x={:.3f}, y={:.3f}".format(xmax, ymax)
if not ax:
ax=plt.gca()
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
arrowprops=dict(arrowstyle="->",connectionstyle="angle,angleA=0,angleB=60")
kw = dict(xycoords='data',textcoords="data",
arrowprops=arrowprops, bbox=bbox_props, ha="left", va="top")
ax.annotate(text, xy=(xmax, ymax), xytext=(xmax+.5,ymax+5), **kw)
#-------------------------------------------------------------
fig=plt.figure()
fig.show()
ax=fig.add_subplot(111)
ax.plot(x1,y1,c='b',ls='-',label='Recovery',fillstyle='none')
ax.plot(x2,y2,c='g',ls='-',label='Normal')
ax.plot(x3,y3,c='r',ls='-',label='No-Recovery')
annot_max(x1,y1)
annot_max(x2,y2)
annot_max(x3,y3)
plt.legend(loc=1)
# naming the x axis
plt.xlabel('<------Instances(count)------>')
# naming the y axis
plt.ylabel('Acceleration (m/sq.sec)')
# giving a title to my graph
plt.title('Fall Detection Comparison graph')
plt.show()
关于python - 多重最大标注 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53879524/