python - Changing tick labels without affecting the plot -


i plotting 2-d array in python using matplotlib , having trouble formatting tick marks. first, data organized 2-d array (elevation, latitude). plotting values of electron density function of height , latitude (basically longitudinal slice @ specific time).

i want label x axis going -90 90 degrees in 30 degree intervals , y values array of elevations (each model run has different elevation values can't manually assign arbitrary elevation). have arrays latitude values in , elevation values both 1-d arrays.

here code:

from netcdf4 import dataset import numpy np import matplotlib.pyplot plt  #load netcdf file variable mar120="c:/users/willevo/desktop/sec_giptie_cpl_mar_120.nc"  #grab data new variable fh=dataset(mar120,mode="r")  #assign model variable contents python variables lons=fh.variables['lon'][:] lats=fh.variables['lat'][:] var1=fh.variables['un'][:]  #specifying time , elevation map ionst=var1[0,:,:,21] ionst=ionst[0:len(ionst)-1]  #close netcdf file fh.close()  #set figure, size, , 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 don't include plt.xticks argument following image bad tick labels:

good image bad tick labeling

if include plt.xticks argument following:

bad figure, data remains static

how can fix this? want data follow change in axis (but accurate). need y axis without manually entering values , instead feeding array of values. thanks

use extent argument of imshow set x , y ranges of image, , use aspect='auto' allow aspect ratio of image adjusted fit figure. example, following code

in [68]: scipy.ndimage.filters import gaussian_filter  in [69]: np.random.seed(12345)  in [70]: = 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 @ 0x1115f02d0>  in [75]: plt.xticks([-90, -60, -30, 0, 30, 60, 90]) out[75]:  ([<matplotlib.axis.xtick @ 0x108307cd0>,   <matplotlib.axis.xtick @ 0x1101c1c50>,   <matplotlib.axis.xtick @ 0x1115d4610>,   <matplotlib.axis.xtick @ 0x1115f0d90>,   <matplotlib.axis.xtick @ 0x1115ff510>,   <matplotlib.axis.xtick @ 0x11161db10>,   <matplotlib.axis.xtick @ 0x111623090>],  <a list of 7 text xticklabel objects>) 

generates plot:

image


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -