plot with custom text for x axis points
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?
Solution 1:
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()
Solution 2:
This worked for me. Each month on X axis
str_month_list = ['January','February','March','April','May','June','July','August','September','October','November','December']
ax.set_xticks(range(0,12))
ax.set_xticklabels(str_month_list)