【问题标题】:Show only exact values of data point on axes with matplotlib figure使用 matplotlib 图仅显示轴上数据点的准确值
【发布时间】:2020-11-08 04:47:42
【问题描述】:

我只想通过 matplotlib 显示坐标轴或数据点坐标上的精确值 (x, y)。我在下面的工作

def plot_sample_individual(id = None):
  if id is None:
    id = random.randint(0, len(ca_the))
  fig, ax = plt.subplots(1, 1, figsize=(5, 5))
  ax.plot(week[:7], ca_the[id, :],'--ro')
  ax.set_title('ID cá thể '+ str(ID[id, 0]))
  ax.set_ylabel('Sản lượng trứng trung bình tuần')
  ax.set_xlabel('Tuần')

代码的结果是:

如何在轴 y 上只显示 3 个值,在轴 x 上只显示 5 个值?

【问题讨论】:

  • exact values 是什么意思?您想在 x 和 y 轴上显示哪些
  • 我想删除不是点坐标的值

标签: python matplotlib


【解决方案1】:

使用x和y数据set the Axes ticks

from matplotlib import pyplot as plt
x = [24,25,26,27,28]
y = [7,4,5,4,4]
fig,ax = plt.subplots()
ax.plot(x,y)
ax.set_xticks(x)
ax.set_yticks(y)
plt.show()
plt.close()

Ticks and tick labels

【讨论】:

  • 但我使用随机 id 来选择样本,所以 x 数组不是唯一的
  • @TungNg - 您没有在 minimal reproducible example 中提供任何数据,所以我自己制作了 - 我示例中的数据点与您的绘图中的数据点匹配。如果您正在寻找更具体的东西,您应该更新您的minimal reproducible example - 包括如何调用该函数。即使您可能在函数中生成id,您仍然可以使用我显示的值。
猜你喜欢
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-05
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多