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?
Use the
xticksfunction. If you dopyplot.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 usenp.arangeto quickly create the range of numbers you want.