0

I'm trying to replicate this: 1

I have a list of words, each of which has an x and y coordinate. I need to graph them just like the one above. What is the best way to do this? I know I could do something like...

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]))

But this doesn't seem too efficient for what I want to do. For each word, I have a function to pass it through to find the x coordinate and then another function to find its y coordinate. Maybe just a list of the words and then a function that loops through and plots each one as it goes through the list? Is this possible?

def plotWords(words):

    fig, ax = pyplot.subplots()

    for w in words:

        ax.annotate(w, code for x coordinate, code for y coordinate)

    return ax
3
  • 2
    Have you tried it this way, and if so are you running into efficiency issues? How many words are you trying to plot? This seems like a decent way to me. Since you tagged the question with pandas, a dataframe would also work nicely. Commented Aug 4, 2020 at 18:47
  • I haven't used the first method because I'll likely keep adding words to my list so I don't want to have to manually insert the x and y coordinates each time. I currently have this code: def plotWords(words, genderPC): fig, ax = pyplot.subplots() for w in words: ax.annotate(w, code for x coordinate, code for y coordinate) return ax but I'm getting an error. Commented Aug 4, 2020 at 20:26
  • The code formatting in comments is horrible so I added my current code to the original post. @MattDMo Commented Aug 4, 2020 at 20:28

2 Answers 2

1

I think you can use zip() to get each of them in a loop.

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)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

This figure

enter image description here

was produced running this script

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()

1 Comment

@MattDMo First of all, thank you. Your comment made me curious and had me check: the OP has accepted an answer on every other question they asked w/o solicitation… who knows…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.