I have following piece of random code that just draws a scatter plot using some random numbers.
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
x = np.random.rand(10)
y = np.random.rand(10)
fig, ax = plt.subplots()
ax.plot(y, x, '*')
plt.show()
And it generates a plot like this:
All good. But what I want is to edit the x labels using some custom labels and I want them to be centered between two x-ticks. Here is how I modified the code to achieve it:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
x = np.random.rand(10)
y = np.random.rand(10)
fig, ax = plt.subplots()
ax.plot(y, x, '*')
xlabels = ['potato','tomato','apple','cherry','pie']
# Remove the major tick labels
ax.xaxis.set_major_formatter(ticker.NullFormatter())
# Set the major ticks to match the number of labels
num_labels = len(xlabels)
mticks = np.arange(num_labels)
ax.set_xticks(mticks)
ax.set_xticklabels(xlabels)
# Rotate the x-axis labels
for label in ax.get_xticklabels():
label.set_rotation(90)
label.set_horizontalalignment('right') # Adjust horizontal alignment
plt.show()
But it generates a plot like this: (don’t mind the scatter plot changing. I guess x and y are random distributed arrays)
How do I edit my code such that the xlabels are at the center in between the ticks.