Python 多重最大标记matplotlib

Python 多重最大标记matplotlib,python,matplotlib,Python,Matplotlib,我试图标记多个最大值,但无法正确显示它们。此外,标签显示距离与图例重叠的点太远 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)

我试图标记多个最大值,但无法正确显示它们。此外,标签显示距离与图例重叠的点太远

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()
导入matplotlib.pyplot作为plt
将numpy作为np导入
x1,y1=np.loadtxt('MaxMin1.txt',dtype=str,unpack=True)
x1=x1.aType(int)
y1=y1.aType(浮动)
x2,y2=np.loadtxt('MaxMin2.txt',dtype=str,unpack=True)
x2=x2.aType(int)
y2=y2.aType(浮动)
x3,y3=np.loadtxt('MaxMin3.txt',dtype=str,unpack=True)
x3=x3.aType(int)
y3=y3.aType(浮动)
#-------------------------------------------------------------
def annot_max(x,y,ax=无):
xmax=x[np.argmax(y)]
ymax=y.max()
text=“x={.3f},y={.3f}”。格式(xmax,ymax)
如果不是ax:
ax=plt.gca()
bbox_props=dict(boxstyle=“square,pad=0.3”,fc=“w”,ec=“k”,lw=0.72)
arrowprops=dict(arrowstyle=“->”,connectionstyle=“角度,角度A=0,角度B=60”)
kw=dict(xycoords='data',textcoords=“轴分数”,
箭头道具=箭头道具,bbox=bbox_道具,ha=“left”,va=“top”)
注释(文本,xy=(xmax,ymax),xytext=(0.94,0.96),**kw)
#-------------------------------------------------------------
图=plt.图()
图2(图3)
ax=图添加_子批次(111)
ax.plot(x1,y1,c='b',ls='-',label='Recovery',fillstyle='none')
ax.绘图(x2,y2,c='g',ls='-',label='Normal')
ax.plot(x3,y3,c='r',ls='-',label='No-Recovery')
annot_最大值(x1,y1)
annot_最大值(x2,y2)
注释最大值(x3,y3)
plt.图例(loc=1)
#命名x轴
plt.xlabel(“”)
#命名y轴
plt.ylabel('加速度(米/平方秒)')
#给我的图表加一个标题
plt.title(‘坠落检测比较图’)
plt.show()
还有我得到的输出
我刚开始学习python,所以很小的提示我可能无法理解。请帮助。

您需要更改函数
注释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}”。格式(xmax,ymax)

导入matplotlib.pyplot作为plt
将numpy作为np导入
x1=np.数组([1,2,3,4,5,6,7,8,9,10])
x2=x1[:]
x3=x1[:]
y1=np.数组([1,2,3100,5,6,7,8,9,10])
y2=np.数组([50,2,3,4,5,6,7,8,9,10])
y3=np.数组([1,2,3,4,5,6,75,8,9,10])
#-------------------------------------------------------------
def annot_max(x,y,ax=无):
xmax=x[np.argmax(y)]
ymax=y.max()
text=“x={.3f},y={.3f}”。格式(xmax,ymax)
如果不是ax:
ax=plt.gca()
bbox_props=dict(boxstyle=“square,pad=0.3”,fc=“w”,ec=“k”,lw=0.72)
arrowprops=dict(arrowstyle=“->”,connectionstyle=“角度,角度A=0,角度B=60”)
kw=dict(xycoords='data',textcoords=“data”,
箭头道具=箭头道具,bbox=bbox_道具,ha=“left”,va=“top”)
ax.annotate(text,xy=(xmax,ymax),xytext=(xmax+.5,ymax+5),**kw)
#-------------------------------------------------------------
图=plt.图()
图2(图3)
ax=图添加_子批次(111)
ax.plot(x1,y1,c='b',ls='-',label='Recovery',fillstyle='none')
ax.绘图(x2,y2,c='g',ls='-',label='Normal')
ax.plot(x3,y3,c='r',ls='-',label='No-Recovery')
annot_最大值(x1,y1)
annot_最大值(x2,y2)
注释最大值(x3,y3)
plt.图例(loc=1)
#命名x轴
plt.xlabel(“”)
#命名y轴
plt.ylabel('加速度(米/平方秒)')
#给我的图表加一个标题
plt.title(‘坠落检测比较图’)
plt.show()

所以我看到的唯一变化是xytext=(xmax+.5,ymax+5)。但是,当我运行此操作时,“最大”标签将超出可见性区域。如果有帮助的话,我正在共享数据文件。在我的操作中,否,在
arrowprops
中更改
textcoords=“data”
。您打印错误。获取我的代码,加载数据并根据需要更改
xytext
位置。我将使用
textcoords=“offset points”
xytext=(25,12)
。当然,您可以使用这些值来查看什么是合适的。将在此处尝试并更新结果。
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()