looking for marker text option for pyplot.plot()

徘徊边缘 提交于 2019-12-04 19:01:39

As jkalden suggested, annotate would solve your problem. The function's xy-argument let you position the text so that you can place it on the marker's position.

About your "zoom" problem, matplotlib will by default stretch the frame between the smallest and largest values you are plotting. It results in your outer markers having their centers on the very edge of the figure, and only half of the markers are visible. To override the default x- and y-limits you can use set_xlim and set_ylim. Here an offset is define to let you control the marginal space.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4 ,5]
y = [1, 4, 9, 6, 10]

fig, ax = plt.subplots()

# instanciate a figure and ax object
# annotate is a method that belongs to axes
ax.plot(x, y, 'ro',markersize=23)

## controls the extent of the plot.
offset = 1.0 
ax.set_xlim(min(x)-offset, max(x)+ offset)
ax.set_ylim(min(y)-offset, max(y)+ offset)

# loop through each x,y pair
for i,j in zip(x,y):
    corr = -0.05 # adds a little correction to put annotation in marker's centrum
    ax.annotate(str(j),  xy=(i + corr, j + corr))

plt.show()

Here is how it looks:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!