如何在图形上用每个点上的文本绘制点(python)
我试图复制这一点: 我有一个单词列表,每个单词都有x和y坐标。我需要像上面一样绘制它们。最好的方法是什么?我知道我可以做一些像如何在图形上用每个点上的文本绘制点(python),python,pandas,matplotlib,Python,Pandas,Matplotlib,我试图复制这一点: 我有一个单词列表,每个单词都有x和y坐标。我需要像上面一样绘制它们。最好的方法是什么?我知道我可以做一些像 y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199] z = [0.15, 0.3, 0.45, 0.6, 0.75] n = [hello, xyz, bbb, fasjd, boy] fig, ax = plt.subplots() for i, txt in enumerate(n): ax.annota
y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [hello, xyz, bbb, fasjd, boy]
fig, ax = plt.subplots()
for i, txt in enumerate(n):
ax.annotate(txt, (z[i], y[i]))
但对于我想做的事情来说,这似乎不太有效。对于每个单词,我都有一个函数来传递它以找到x坐标,然后是另一个函数以找到它的y坐标。也许只是一个单词列表,然后是一个函数,在遍历列表时循环并绘制每个单词?这可能吗
def plotWords(words):
fig, ax = pyplot.subplots()
for w in words:
ax.annotate(w, code for x coordinate, code for y coordinate)
return ax
我认为您可以使用
zip()
将它们都放入一个循环中
import matplotlib.pyplot as plt
y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = ['hello', 'xyz', 'bbb', 'fasjd', 'boy']
fig = plt.figure(figsize=(4,3),dpi=144)
ax = fig.add_subplot(111)
def plotWords(words,z,y):
for w,xx,yy in zip(words,z,y):
ax.annotate(w, xy=(xx,yy))
ax.set_ylim(0,5)
plt.show()
return
plotWords(n,z,y)
这个数字
是运行此脚本生成的
import matplotlib.pyplot as plt
import numpy as np
# determine the sequence of random numbers
np.random.seed(sum(ord(c) for c in 'gboffi'))
# initialize the plotting
fig, ax = plt.subplots(figsize=(8,6), constrained_layout=1)
# a list of long words
N = 40
data = '/usr/share/dict/italian'
long_words = [w.strip() for w in open(data) if len(w)>12 and "'" not in w]
words = np.random.choice(long_words, N)
# let's compute the word characteristics
xs = np.random.random(N) + [len(w) for w in words]
ys = np.random.random(N) + [sum(ord(c) for c in w)/len(w) for w in words]
# determine the axes limits
xmn = min(xs) ; xmx = max(xs) ; dx = xmx-xmn
ymn = min(ys) ; ymx = max(ys) ; dy = ymx-ymn
ax.set_xlim(xmn-0.05*dx, xmx+0.05*dx) ; ax.set_ylim(ymn-0.05*dy, ymx+0.05*dx)
# label the axes
plt.xlabel('Word Length', size='x-large')
plt.ylabel('Mean Value', size='x-large')
# for every word, plot a red "o" and place the text around it
for w, x, y in zip(words, xs, ys):
ax.plot((x,),(y,), 'o', c='r')
ax.annotate(w, (x, y), ha='center', va='center')
# the dashed red lines, the position is a little arbitrary
plt.hlines(ymn+0.5*dy, xmn, xmx, ls='--', color='r')
plt.vlines(xmn+0.4*dx, ymn, ymx, ls='--', color='r')
# remove everything except points, red lines and axes' labels
plt.box(False)
plt.tick_params(left=0, top=0, right=0, bottom=0,
labelleft=0, labeltop=0, labelright=0, labelbottom=0)
# we are done
plt.show()
您是否尝试过这种方式,如果是,您是否遇到了效率问题?你想画多少字?这对我来说是个不错的方式。由于您将问题标记为
pandas
,因此数据框也可以很好地工作。我没有使用第一种方法,因为我可能会不断向列表中添加单词,因此我不想每次都手动插入x和y坐标。我现在有这样一个代码:def plotWords(words,genderPC):fig,ax=pyplot.subplot()用于words:ax.annotate(w,x坐标的代码,y坐标的代码)返回ax
,但我得到了一个错误。注释中的代码格式很糟糕,所以我将当前代码添加到了原始帖子中@MattDMo@curlypie99如果这个答案,请考虑点击在左边的检查标记/滴答,把它变成绿色。这标志着问题已解决,令您满意,并奖励您和回答者。一旦你获得>=15个声望点,如果你愿意,你也可以对答案进行投票。“我们也没有义务这么做。”马特莫首先,谢谢你。你的评论让我好奇,并让我检查:OP已经接受了他们提出的每一个问题的答案,没有征求意见……谁知道…