如何在每个点上用文本绘制图形上的点(python)

我正在尝试复制这个:

http://img1.mukewang.com/6447808e00012d3a06470295.jpg

我有一个单词列表,每个单词都有一个 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.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


精慕HU
浏览 88回答 2
2回答

蓝山帝景

我想你可以用zip()它们来循环。import matplotlib.pyplot as plty = [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 plottingfig, 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 characteristicsxs = 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 limitsxmn = 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 axesplt.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 arbitraryplt.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' labelsplt.box(False)plt.tick_params(left=0, top=0, right=0, bottom=0,                labelleft=0, labeltop=0, labelright=0, labelbottom=0) # we are doneplt.show()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python