I am plotting data with strings as x labels. I'd like to control the label frequency to not overload the axis with text. In the example below i'd like to only see a label every 3 tick: a-d-g-j.
One way I could do that is to replace the my_xticks elements by 2 empty strings every n elements. But i'm sure it gives a cleaner way to do that.
Here is the code:
import numpy as np
import matplotlib.pyplot as plt
y=np.array([4,4,4,5,5,6,5,5,4,4,4])
x = np.arange(y.shape[0])
my_xticks=['a','b','c','d','e','f','g','h','i','j','k']
plt.xticks(x, my_xticks)
plt.plot(x, y)
You're almost there! Sample the first and second arguments of xticks at the desired frequency!
The question is a duplicate from Changing the "tick frequency" on x or y axis in matplotlib?
import numpy as np
import matplotlib.pyplot as plt
y = np.array([4,4,4,5,5,6,5,5,4,4,4])
x = np.arange(y.shape[0])
my_xticks = np.array(['a','b','c','d','e','f','g','h','i','j','k'])
frequency = 3
plt.plot(x, y)
plt.xticks(x[::frequency], my_xticks[::frequency])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With