简体   繁体   English

带有批注的matplotlib图上竞争的刻度线集

[英]Competing sets of tick marks on a matplotlib plot with annotations

Given the following matplotlib snippet that uses annotations : 给定以下使用annotations matplotlib代码段:

labels = ['Point-%d' %i for i in range(0,10)]
outMat = np.array([(-20 + 20*x + 20*np.sin(math.pi * 2 * x), -30 + x*30 + np.sin(math.pi * 2 * x)) for x in range(0,10)])
fig = plt.figure()
plt.title(title)
ax = fig.subplots(1,1)
for i in range(outMat.shape[0]):
  ax.annotate(labels[i],outMat[i],(outMat[i][0],outMat[i][1]))
plt.scatter(outMat[:,0],outMat[:,1])
fig.show()

We end up with overlapping/competing y and x tick labels: 我们最终重叠/竞争 yx刻度标签:

The correctly scaled ticks from [0.0 to 250.0] - based on the range of the input x and y values- should be the only ones. 根据输入的xy值的范围,从[0.0到250.0]正确缩放的刻度应该是唯一的刻度。 It is unclear why there are tick marks of [0.0-1.0] in 0.2 increments: the request here is to remove those spurious marks. 目前尚不清楚为什么以0.2为增量有[0.0-1.0]的刻度线:这里的要求是删除那些虚假标记。

在此处输入图片说明

What needs to change in the snippet? 摘要中需要更改什么?

The problem is that you add a subplot to your figure, which adds another set of labels. 问题是您在图形中添加了子图,从而添加了另一组标签。 You prevent this, by using only plt.subplots : 通过仅使用plt.subplots可以防止这种plt.subplots

import numpy as np
fig, ax = plt.subplots(1,1)
labels = ['Point-%d' %i for i in range(0,10)]
outMat = np.array([(-20 + 20*x + 20*np.sin(np.pi * 2 * x), -30 + x*30 + np.sin(np.pi * 2 * x)) for x in range(0,10)])
plt.scatter(outMat[:,0],outMat[:,1])
for i in range(outMat.shape[0]):
    ax.annotate(labels[i],outMat[i],(outMat[i][0],outMat[i][1]))
plt.title("title")
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM