How To Adjust X Axis In Matplotlib
I have a graph like this The data on the x axis means hours, so I want the x axis to set as 0, 24, 48, 72.....instead of the value now, which is difficult to see the data between [
Solution 1:
Look at the docs :
xlocs, xlabs = plt.xticks()
put in xlocs your range, and in xlabs what you want to display.
then:
plt.xticks(xlocs, xlabs)
Solution 2:
It sounds like you want to changes the limits of the plotting display - for that use xlim
(and ylim
for the other axis). To change the xticks themselves is provided in the answer by @fp. Show below is an example using without/with xlim
:
# Sample dataimport numpy as np
N = 2000
X = np.random.gamma(.5,size=N)*100import pylab as plt
plt.subplot(2,1,1)
plt.hist(X,bins=300)
plt.subplot(2,1,2)
plt.hist(X,bins=300)
plt.xlim(0,100)
plt.show()
Solution 3:
The size of the plot can be changed by setting the dynamic rc settings of Matplotlib. These are stored in a dictionary named rcParams. The size of the plot figure is stored with the key figure.figsize.
As an example, to get and set the size of a Matplotlib plot: import matplotlib.pyplot as plt
fig_size = plt.rcParams["figure.figsize"] #Get current sizeprint"Current size:", fig_size #Prints: [8.0, 6.0]
fig_size[0] = 12 #Set figure width to 12 and height to 9
fig_size[1] = 9
plt.rcParams["figure.figsize"] = fig_size
Post a Comment for "How To Adjust X Axis In Matplotlib"