plot with custom text for x axis points


Question

I am drawing a plot using matplotlib and python like the sample code below.

x = array([0,1,2,3])
y = array([20,21,22,23])
plot(x,y)
show()

As it is the code above on the x axis I will see drawn values 0.0, 0.5, 1.0, 1.5 i.e. the same values of my reference x values.

Is there anyway to map each point of x to a different string? So for example I want x axis to show months names( strings Jun, July,...) or other strings like people names ( "John", "Arnold", ... ) or clock time ( "12:20", "12:21", "12:22", .. ).

Do you know what I can do or what function to have a look at?
For my purpose could it be matplotlib.ticker of help?

1
94
4/19/2015 5:37:11 PM

Accepted Answer

You can manually set xticks (and yticks) using pyplot.xticks:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0,1,2,3])
y = np.array([20,21,22,23])
my_xticks = ['John','Arnold','Mavis','Matt']
plt.xticks(x, my_xticks)
plt.plot(x, y)
plt.show()

169
5/11/2019 2:22:02 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon