1

(towns_n and Towns are two arrays each with 50 numbers and names respectively)

count = 0 
for number,town in zip(towns_n,Towns):
    textString += (number +'.'+ town).ljust(35)
    count += 1
    if count == 6:
        count = 0
        textString += '\n' 
plt.figtext(0.13,0.078,textString)

My problem is that I want to plot 6 columns.

And if I print my string it looks exactly as expected, it looks like 6 aligned columns. But if I plot it along my other image it doesn't look aligned at all.
I don't think it matters but my other image is a map usign basemap. I am plotting this string just below my map.

What I am getting

Edit: You can try this to generate 50 random strings and numbers so that you don't need the actual list of towns.

def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

Towns=[]
towns_n=[]

for i in range(50):
    string = id_generator()
    Towns.append(string);
    towns_n.append(str(i))
3
  • Can you please provide an image of what you are talking about here? Also, provide a minimal reproducible example of the issue. Commented Nov 2, 2017 at 23:33
  • I guess what you are trying to do requires a monospaced font. Did you consider using one? Otherwise you would need to create 6 single text elements (one for each "column"). Again, provide a minimal reproducible example to get an answer (or are you expecting someone to type in all cities in the UK?). Commented Nov 2, 2017 at 23:48
  • Thanks, I think I am about to give up and do what you suggest about the 6 strings. I have added something in the last edit so that you can get 50 random words and numbers. Commented Nov 3, 2017 at 0:30

1 Answer 1

1

As said in the comments, one solution would be to use a monospaced font.

plt.figtext(..., fontname="DejaVu Sans Mono")

Example:

import random
import string
import matplotlib.pyplot as plt

def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

Towns=[]
towns_n=[]

for i in range(50):
    string = id_generator()
    Towns.append(string);
    towns_n.append(str(i))


count = 0 
textString =""
for number,town in zip(towns_n,Towns):
    textString += (number +'.'+ town).ljust(12)
    count += 1
    if count == 6:
        count = 0
        textString += '\n'

fig = plt.figure()
fig.add_subplot(111)
fig.subplots_adjust(bottom=0.5)
plt.figtext(0.05,0.078,textString, fontname="DejaVu Sans Mono")

plt.show()

enter image description here

The other option is to create each column separately:

import random
import string
import matplotlib.pyplot as plt

def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

Towns=[]
towns_n=[]

for i in range(50):
    string = id_generator()
    Towns.append(string);
    towns_n.append(str(i))


fig = plt.figure()
fig.add_subplot(111)
fig.subplots_adjust(bottom=0.5)

space = 0.05
left = 0.05
width= 0.15
for i in range(6):
    t = [towns_n[i::6][j] + ". " + Towns[i::6][j] for j in range(len(towns_n[i::6]))]
    t = "\n".join(t)
    plt.figtext(left+i*width,0.35,t, va="top", ha="left")

plt.show()

enter image description here

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

Comments

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.