I am making a figure with pyplot in which a string is printed, with each character put in the x position of its index in the string. I am saving the figure as a pdf, but somehow spaces are added between some characters.
Here's a minimal code:
import matplotlib.pyplot as plt
string = ("QWERTZUIOPASDFGHJKLYXCVBNM")
plt.xlim(0,len(string))
for index, letter in enumerate(string):
plt.text(index, .5, letter)
plt.savefig("fig.pdf")
If I run the code, in the final pdf there's a space between the first and second letter (Q and W), between the third and fourth (E and R) and so on.
Anyone has an idea of why this is happening and how I could avoid it?
Obviously an I
is much less wide than a W
. So spacings are different. If you want to distribute those spacing more equally, consider center aligning the letters
plt.text(index, .5, letter, ha="center")
Also, consider using a monospace font, ie a font where each letter is equal in width, eg
plt.text(index, .5, letter, ha="center",
fontproperties=FontProperties(family='Consolas'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.