Search code examples
pythonnumpymultidimensional-arraymatplotlibaxes

Changing tick labels without affecting the plot


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:

Good Image but bad tick labeling

If I include the plt.xticks argument I get the following:

Bad figure, data remains static

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


Solution

  • Use the extent argument of imshow to set the x and y ranges of the image, and use aspect='auto' to allow the aspect ratio of the image to be adjusted to fit the figure. For example, the following code

    In [68]: from scipy.ndimage.filters import gaussian_filter
    
    In [69]: np.random.seed(12345)
    
    In [70]: a = np.random.randn(27, 36)
    
    In [71]: b = gaussian_filter(a, 4)
    
    In [72]: ymin = 0
    
    In [73]: ymax = 1
    
    In [74]: plt.imshow(b, origin='lower', extent=[-90, 90, ymin, ymax], aspect='auto')
    Out[74]: <matplotlib.image.AxesImage at 0x1115f02d0>
    
    In [75]: plt.xticks([-90, -60, -30, 0, 30, 60, 90])
    Out[75]: 
    ([<matplotlib.axis.XTick at 0x108307cd0>,
      <matplotlib.axis.XTick at 0x1101c1c50>,
      <matplotlib.axis.XTick at 0x1115d4610>,
      <matplotlib.axis.XTick at 0x1115f0d90>,
      <matplotlib.axis.XTick at 0x1115ff510>,
      <matplotlib.axis.XTick at 0x11161db10>,
      <matplotlib.axis.XTick at 0x111623090>],
     <a list of 7 Text xticklabel objects>)
    

    generates this plot:

    image