我想给每个 x 点一个标签。从 0 到 Inf.
标签应该在最高处可见。
功能:
def plot_pics(self, figure,title, x, a1, a2, a3, labelx, labely):
ax = figure.add_subplot(111)
ax.plot(x,a1,'-o')
ax.plot(x,a2,'-o')
ax.plot(x,a3,'-o')
ax.legend(['left region','center region','right region'])
ax.set_xlabel(labelx)
ax.set_ylabel(labely)
ax.set_title(title)
figure.canvas.draw_idle()
最佳答案
这是我认为您想要实现的目标的最小工作示例。
import numpy as np
import matplotlib.pyplot as plt
# Random plotting data
x_arr = np.arange(10)
rand_arr = np.random.random((10, 3))
# Plot everything
plt.plot(x_arr, rand_arr, '-o')
# Find maximas for every x-value
rand_max_arr = np.max(rand_arr, axis=1)
x_offset = 0.5
y_offset = 0.04
for x, y in zip(x_arr, rand_max_arr):
plt.text(x - x_offset, y + y_offset, "point {:d}".format(x), bbox=dict(facecolor="white"))
plt.show()
出于测试目的,我创建了 3 个数组,每个数组包含 10 个随机数。之后,您必须找到每个 x 点的最大值,并通过 plt.text()
将文本附加到该点,而坐标是 x 点和找到的最大值。偏移量用于移动文本,因此它对绘制的最大值本身的干扰最小。
关于Python Matplotlib -> 给每个 x 轴一个数字标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74140671/