[英]Label python data points on plot
我搜索了年齡(幾個小時,就像年齡一樣)找到一個非常煩人(看似基本)問題的答案,因為我找不到一個非常適合答案的問題,我發布一個問題並回答它,希望它我將花費大量時間用於我的noobie繪圖技巧。
如果你想使用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()
我知道xytext =(30,0)與textcoords一起使用,你使用那些30,0值來定位數據標簽點,所以它在0 y軸上,30在x軸上超過它自己的小區域。
您需要繪制i和j的兩條線,否則您只繪制x或y數據標簽。
你得到這樣的東西(僅注意標簽):
它不理想,仍然有一些重疊 - 但它比我所擁有的更好。
如何立即打印(x, y)
。
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()
我有一個類似的問題,最后得到了這個:
對我來說,這具有數據和注釋不重疊的優點。
from matplotlib import pyplot as plt
import numpy as np
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)
# annotations at the side (ordered by B values)
x0,x1=ax.get_xlim()
y0,y1=ax.get_ylim()
for ii, ind in enumerate(np.argsort(B)):
x = A[ind]
y = B[ind]
xPos = x1 + .02 * (x1 - x0)
yPos = y0 + ii * (y1 - y0)/(len(B) - 1)
ax.annotate('',#label,
xy=(x, y), xycoords='data',
xytext=(xPos, yPos), textcoords='data',
arrowprops=dict(
connectionstyle="arc3,rad=0.",
shrinkA=0, shrinkB=10,
arrowstyle= '-|>', ls= '-', linewidth=2
),
va='bottom', ha='left', zorder=19
)
ax.text(xPos + .01 * (x1 - x0), yPos,
'({:.2f}, {:.2f})'.format(x,y),
transform=ax.transData, va='center')
plt.grid()
plt.show()
在.annotate
使用text參數最終會產生不利的文本位置。 在圖例和數據點之間繪制線條很麻煩,因為圖例的位置很難解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.