0

I am using the following code to make some plot of a variable having 4 values:

for station in stations:
    os.chdir(inbasedir)
    nc = Dataset(surf + station + "Fluxnet.nc", 'r+')
    soil_moist = nc.variables['SoilMoist'][:,0,0]
    plt.plot(soil_moist, c='black', linewidth=0.5, label="Soil Moisture - 4 layers")

Which gives me the following plot:

enter image description here

How could I modify the xticks as follow:

  • how to replace the 0 by 1, the 1 by 2, the 2 by 3 and the 3 by 4?
    • how can I remove the 0.5, 1.5, 2.5 ?
    • how can I get rid of the floating point numerotation?

I tried this answer: Changing the "tick frequency" on x or y axis in matplotlib?

but it does not work and provides me the following error: TypeError: arange: scalar arguments expected instead of a tuple.

steve
  • 511
  • 1
  • 9
  • 33
  • This may help https://matplotlib.org/api/_as_gen/matplotlib.pyplot.xticks.html – Carlos Gonzalez Oct 24 '18 at 13:05
  • Possible duplicate of [Changing the "tick frequency" on x or y axis in matplotlib?](https://stackoverflow.com/questions/12608788/changing-the-tick-frequency-on-x-or-y-axis-in-matplotlib) – Carlos Gonzalez Oct 24 '18 at 13:06

2 Answers2

2

The xticks method of matplotlib.pyplot expects an array for the values to display as first argument and an array with labels for those elements in the first array. So add the following to your code:

plt.xticks(positions, labels)

Where positions is the array of the values you want to display, and labels the labels you want to give to those values.

SBylemans
  • 1,764
  • 13
  • 28
1

Make sure to plot the actual data

If 0 really denotes 1, you should plot 1 in the first place.

x = [1,2,3,4]
y = [.3,.3,.25,.29]

plt.plot(x,y)
plt.show()

enter image description here

Set the locations to integer numbers

from matplotlib.ticker import MultipleLocator

x = [1,2,3,4]
y = [.3,.3,.25,.29]

plt.plot(x,y)

plt.gca().xaxis.set_major_locator(MultipleLocator(1))
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712