Python Matplotlib -> 给每个 x 轴一个数字标签

标签 python matplotlib label

我想给每个 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()

Plot

最佳答案

这是我认为您想要实现的目标的最小工作示例。

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()

它生成以下图。 enter image description here

出于测试目的,我创建了 3 个数组,每个数组包含 10 个随机数。之后,您必须找到每个 x 点的最大值,并通过 plt.text() 将文本附加到该点,而坐标是 x 点和找到的最大值。偏移量用于移动文本,因此它对绘制的最大值本身的干扰最小。

关于Python Matplotlib -> 给每个 x 轴一个数字标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74140671/

相关文章:

python - 如何对齐MatPlotLib中包含上下索引的刻度标签?

ruby-on-rails - 在 Rails 中,label 和 label_tag 有什么区别?

css - 如何用 CSS 控制 <label> ?

python - Seaborn Facetgrid countplot 色调

python - 如何为 Scikit Learn 重新格式化分类 Pandas 变量

python - 在python中删除列表中的素数索引位置

python - 两组虚拟变量的频率/列联表

python - 如何在堆叠直方图中排列箱,Python

python - 在绘制新图之前,如何从 matplotlib 图中删除点?

python - 是否可以将文本隐藏在 Tkinter 的按钮/标签内?