I am plotting a 2-D array in python using matplotlib and am having trouble formatting the tick marks. So first, my data is currently organized as a 2-D array with (elevation, latitude). I am plotting values of electron density as a function of height and latitude (basically a longitudinal slice at a specific time).
I want to label the x axis going from -90 to 90 degrees in 30 degree intervals and the y values with another array of elevations (each model run has different elevation values so I can't manually assign an arbitrary elevation). I have arrays with latitude values in it and another with elevation values both 1-D arrays.
Here is my code:
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
#load the netcdf file into a variable
mar120="C:/Users/WillEvo/Desktop/sec_giptie_cpl_mar_120.nc"
#grab the data into a new variable
fh=Dataset(mar120,mode="r")
#assign model variable contents to python variables
lons=fh.variables['lon'][:]
lats=fh.variables['lat'][:]
var1=fh.variables['UN'][:]
#specifying which time and elevation to map
ionst=var1[0,:,:,21]
ionst=ionst[0:len(ionst)-1]
#close the netCDF file
fh.close()
#Set the figure, size, and resolution
plt.figure(figsize=(8,6), dpi=100, facecolor='white')
plt.subplot(1,1,1)
plt.imshow(ionst, origin='lower', interpolation='spline16')
plt.xticks([-90, -60, -30, 0, 30, 60, 90])
plt.show()
If I don't include the plt.xticks argument I get the following good image but bad tick labels:
If I include the plt.xticks argument I get the following:
How can I fix this? I want the data to follow the change in axis (but be accurate). I also need to do this for the y axis but without manually entering values and instead feeding an array of values. Thanks