Luigi
Luigi

Reputation: 341

How to change x-axis ticks in matplot.pyplot?

I have these lines of code that produce the simple graph you see. I need the graph as is, however, I need to replace the numeric ticks with the months of the year contained in the labels list. THE tick for the month of July needs to be roughly the one in the middle. Millimeter accuracy is not necessary. The important thing is to set the labels correctly. How could I solve this? Possibly, if the axis is too small, April and October can also be removed from the list.

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
import statistics
  
# Plot between -10 and 10 with .001 steps.
x_axis = np.arange(-21, 21, 1)
#print(x_axis)
  
# Calculating mean and standard deviation
mean = statistics.mean(x_axis)
sd = 0.5*statistics.stdev(x_axis)
labels =["Apr","May","Jun","Jul","Aug","Sep","Oct"]

plt.plot(x_axis, norm.pdf(x_axis, mean, sd),'ro',color='b',linewidth=3)
plt.plot(x_axis, norm.pdf(x_axis, mean-0.3, sd-0.3),color='r',linewidth=2.5)
plt.show()

enter image description here

Upvotes: 1

Views: 5504

Answers (1)

Stef
Stef

Reputation: 30579

You can set xticks and their labels with plt.xticks:

plt.plot(x_axis, norm.pdf(x_axis, mean, sd),'o', color='b',linewidth=3)
plt.plot(x_axis, norm.pdf(x_axis, mean-0.3, sd-0.3),color='r',linewidth=2.5)
plt.xticks(np.linspace(-20, 20, len(labels)), labels)
plt.show()

enter image description here

Upvotes: 4

Related Questions