python用小矩形标记数据点_在图上标记python数据点

I searched for ages (hours which is like ages) to find the answer to a really annoying (seemingly basic) problem, and because I cant find a question that quite fits the answer I am posting a question and answering it in the hope that it will save someone else the huge amount of time I just spent on my noobie plotting skills.

If you want to label your plot points using python matplotlib

from matplotlib import pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)

A = anyarray

B = anyotherarray

plt.plot(A,B)

for i,j in zip(A,B):

ax.annotate('%s)' %j, xy=(i,j), xytext=(30,0), textcoords='offset points')

ax.annotate('(%s,' %i, xy=(i,j))

plt.grid()

plt.show()

I know that xytext=(30,0) goes along with the textcoords, you use those 30,0 values to position the data label point, so its on the 0 y axis and 30 over on the x axis on its own little area.

You need both the lines plotting i and j otherwise you only plot x or y data label.

You get something like this out (note the labels only):

Its not ideal, there is still some overlap - but its better than nothing which is what I had..

解决方案

How about print (x, y) at once.

from matplotlib import pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)

A = -0.75, -0.25, 0, 0.25, 0.5, 0.75, 1.0

B = 0.73, 0.97, 1.0, 0.97, 0.88, 0.73, 0.54

plt.plot(A,B)

for xy in zip(A, B): #

ax.annotate('(%s, %s)' % xy, xy=xy, textcoords='data') #

plt.grid()

plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值