Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在matplotlib绘图中指定点的坐标?_Python_Matplotlib - Fatal编程技术网

Python 如何在matplotlib绘图中指定点的坐标?

Python 如何在matplotlib绘图中指定点的坐标?,python,matplotlib,Python,Matplotlib,当我绘制一个函数(基于np.array)时,某些值在绘图中有它们的坐标,我如何才能做到这一点? 我知道如何使用代码行更改颜色和其他小事情,例如: line1, = plt.plot(t, f, '*-', label='force', color='#4F81BD') # blue line2, = plt.plot(t, a, 'o-', label='acceleration', color='#C0504D') # red 但是,例如,如果我在绘图线中有一个“峰值”,我不知道如何使它

当我绘制一个函数(基于np.array)时,某些值在绘图中有它们的坐标,我如何才能做到这一点? 我知道如何使用代码行更改颜色和其他小事情,例如:

line1, = plt.plot(t, f, '*-', label='force', color='#4F81BD')  # blue
line2, = plt.plot(t, a, 'o-', label='acceleration', color='#C0504D')  # red

但是,例如,如果我在绘图线中有一个“峰值”,我不知道如何使它们的坐标显示在同一个绘图中

此代码片段可能会帮助您:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

x=[1,2,3,4,5,6,7,8,9,10]
y=[1,1,1,2,10,2,1,1,1,1]
line, = ax.plot(x, y)

ymax = max(y)
xpos = y.index(ymax)
xmax = x[xpos]

#Labeling the graph (ymax+1 is defining the distance from the word to the point)   
ax.annotate('local max', xy=(xmax, ymax), xytext=(xmax, ymax+1))

ax.set_ylim(0,20)
plt.show()
输出:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

x=[1,2,3,4,5,6,7,8,9,10]
y=[1,1,1,2,10,2,1,1,1,1]
line, = ax.plot(x, y)

ymax = max(y)
xpos = y.index(ymax)
xmax = x[xpos]

#Labeling the graph (ymax+1 is defining the distance from the word to the point)   
ax.annotate('local max', xy=(xmax, ymax), xytext=(xmax, ymax+1))

ax.set_ylim(0,20)
plt.show()


我希望我能帮你一点忙。

这个代码片段可能会帮助你:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

x=[1,2,3,4,5,6,7,8,9,10]
y=[1,1,1,2,10,2,1,1,1,1]
line, = ax.plot(x, y)

ymax = max(y)
xpos = y.index(ymax)
xmax = x[xpos]

#Labeling the graph (ymax+1 is defining the distance from the word to the point)   
ax.annotate('local max', xy=(xmax, ymax), xytext=(xmax, ymax+1))

ax.set_ylim(0,20)
plt.show()
输出:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

x=[1,2,3,4,5,6,7,8,9,10]
y=[1,1,1,2,10,2,1,1,1,1]
line, = ax.plot(x, y)

ymax = max(y)
xpos = y.index(ymax)
xmax = x[xpos]

#Labeling the graph (ymax+1 is defining the distance from the word to the point)   
ax.annotate('local max', xy=(xmax, ymax), xytext=(xmax, ymax+1))

ax.set_ylim(0,20)
plt.show()


我希望我能帮你一点忙。

你说的“显示他们的坐标”是什么意思?你是说轴上的X点吗?特殊标记?文本标签?还有别的吗?也许可以添加一个说明你的目标……你说的“显示他们的坐标”是什么意思?你是说轴上的X点吗?特殊标记?文本标签?还有别的吗?也许可以添加一个说明您的目标……也许我被夸大了,但OP明确表示绘图基于
np.array
,因此我认为您必须使用它,而不是
list
,您没有夸大。谢谢你提到那件事。上述代码确实不能用于2D、3D等数组(例如[1、2、3]、[4、5、6]])。但我想你可以根据自己的喜好修改代码。这实际上很有帮助,谢谢你们两位。我可能被夸大了,但OP明确表示绘图是基于
np.array
,所以我认为你必须使用它,而不是
list
,你一点也不夸张。谢谢你提到那件事。上述代码确实不能用于2D、3D等数组(例如[1、2、3]、[4、5、6]])。但我想你们可以根据自己的喜好修改代码。这实际上非常有用,谢谢你们