4

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

Will.Evo
  • 1,112
  • 13
  • 31
  • Does this entry help? http://stackoverflow.com/questions/18696122/change-values-on-matplotlib-imshow-graph-axis – N1B4 Jun 02 '15 at 23:54

1 Answers1

5

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

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
  • Thanks a ton! There is so much documentation for matplotlib that sometimes finding what you need is a nightmare. Saved me hours of searching. Thanks! – Will.Evo Jun 03 '15 at 14:07
  • I know it's been a while, but I'm looking into how I can suppress matplotlib from printing the `matplotlib.axis.XTick at 0x...` rows in the notebook. Any ideas? Thanks. – Optimesh May 08 '19 at 15:01
  • 1
    I'm going to answer my own question in case anyone every needs this: either end the last drawing statement with a semi-colon or just add a `plt.show()` after all of the drawing statements. – Optimesh May 08 '19 at 17:57