Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make the x-coordinates letters?

I have made a line graph in python with pyplot/matplotlib:

import matplotlib.pyplot as plt
import math
import numpy as np

alphabet = range(0, 25)
firstLine = [letter + 65 for letter in alphabet]
secondLine = [letter + 97 for letter in alphabet]

plt.plot(alphabet, firstLine, '-b', label='ASCII value of capital.')
plt.plot(alphabet, secondLine, '--g', label='ASCII value of lowercase.')
plt.xlabel('Letter in Alphabet')
plt.ylabel('ASCII Value')
plt.title('ASCII value vs. Letter')
plt.legend()
plt.show()

On my x-axis, it current scales by numbers. However, I want increments on the x-axis to be labeled by letters (a, b, c, d) instead of say 0, 5, 10... Specifically, I want the letter 'a' to map to 0, 'b' to map to 1, etc.

How do I make pyplot do that?

like image 879
dangerChihuahua007 Avatar asked Sep 26 '12 05:09

dangerChihuahua007


People also ask

How do you write X in coordinates?

The order in which you write x- and y-coordinates in an ordered pair is very important. The x-coordinate always comes first, followed by the y-coordinate. As you can see in the coordinate grid below, the ordered pairs (3,4) and (4,3) are two different points!

How do you write X and Y in coordinates?

The x coordinate always comes first, followed by the y coordinate. That is why (3, 4) is not the same as (4, 3). (3, 4) refers to a point three units to the right of zero and four units above zero. (4, 3) refers to a point four units to the right of zero and three units above zero.

What order do you write XYZ coordinates?

The three-dimensional rectangular coordinate system is the one-to-one correspondence be- tween each point P in three-dimensional space, or xyz-space, and an ordered triple (x, y, z) in 3. The numbers x, y and z are the x-, y- and z-coordinates of P. The origin O is the point with coordinates (0,0,0).


2 Answers

Use the xticks function. If you do pyplot.xticks([0, 1, 2, 3], ['a', 'b', 'c', 'd']) then it will have axis marks at 0, 1, 2, and 3, and they will be labeled a, b, c, and d. You can also use np.arange to quickly create the range of numbers you want.

like image 71
BrenBarn Avatar answered Oct 06 '22 14:10

BrenBarn


I've used plt.xticks(range(26), [chr(97 + x) for x in xrange(26)]) below.

NOTE: you should change your alphabet = range(0, 25) to alphabet = range(26) because otherwise you are missing out on 'z'.

enter image description here

like image 44
wim Avatar answered Oct 06 '22 14:10

wim