How to make letters of x-coordinates?

I 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, the current scale is measured in numbers. However, I want the increments on the x axis to be marked with the letters (a, b, c, d) instead of saying 0, 5, 10 ... In particular, I want the letter "a" to appear in 0, b 'to display 1, etc.

How can i make pyplot?

+6
source share
2 answers

Use the xticks function. If you do pyplot.xticks([0, 1, 2, 3], ['a', 'b', 'c', 'd']) then it will have axis labels 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 a range of numbers.

+10
source

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

NOTE: you should change alphabet = range(0, 25) to alphabet = range(26) , because otherwise you will not specify "z".

enter image description here

+5
source

Source: https://habr.com/ru/post/926276/


All Articles