xticks and labels stuck on one side of the subplot

Question:

I am plotting 3 maps on one figure. For some reason when I go to label the xaxis the numbers are all crammed on one side of the plot. Is there anyone to space the values out?

for j in xrange(0,3):
        data = mydatalist[j]
        a.append(fig.add_subplot(3,2,j+1))]
        m.append(Basemap(projection='mill', llcrnrlat=-90, urcrnrlat=90, 
            llcrnrlon=30,urcrnrlon=390, resolution='c', ax=a[j]))

        x=np.linspace(30,390,288)
        y = np.linspace(-90, 90, 234)                                                                                                                
        x, y = np.meshgrid(x, y)                                                                                                                     
        x, y = m[j](x,y)                                                                                                                             
        cintervals = [-0.1,-0.09, -0.08, -0.07, -0.06,-0.05, -0.04, -0.03, -0.02,-0.01,                                                             
                 0, 0.01,0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.09,0.1]                                                                                

        mesh = m[j].contourf(x,y,data,cintervals, cmap=plt.cm.jet)
        xlab=np.concatenate([np.arange(30,181,30),np.arange(-150,31,30)])
        plt.xticks(np.linspace(30, 390, 13),xlab)
        plt.tick_params(labelsize=8) 

plt.show()

enter image description here

Asked By: wxcoder

||

Answers:

Switching to a Gall Stereographic projection solved the problem for me, although i’m not sure why it does not work on a Miller projection.

Answered By: wxcoder

Your problem is with co-ordinate mismatch between map coordinates and lat / long

You assign your x ticks to be displayed along the x axis spaced according to

np.linspace(30, 390, 13)

However – if you look at your values in x (i.e. the actual x co-ordinates that you are plotting against in the contourf line), you see they run from 0 to 40030154.74248523.

To avoid this – replace

plt.xticks(np.linspace(30, 390, 13),xlab)

with

plt.xticks(np.linspace(min(x[0]),max(x[0]), len(xlab)),xlab)

Note – you can produce this effect with a lot smaller but complete example, which might have helped you to isolate the issue. Take a look at how to produce a Minimal, complete and verifiable example. As it stands, your code doesn’t run as it is missing a, m, mydatalist and the required imports.

I’ve put in the code below that you might have provided – retaining the subplot loop – although in reality you will likely get the same effect even with just one plot, rather than subplots.

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np

x=np.linspace(30,390,288)
y = np.linspace(-90, 90, 234)                                                                                                                
xg, yg = np.meshgrid(x, y)                                                                                                                     

fig = plt.figure()

for j in xrange(0,3):
        a = fig.add_subplot(3,2,j+1)
        m = Basemap(projection='mill', llcrnrlat=-90, urcrnrlat=90, llcrnrlon=30,urcrnrlon=390, resolution='c', ax=a)
        m.drawcoastlines() # Just put something on the map - doesn't need to be your complex contour plot
        x, y = m(xg,yg)    

        #You can see the problem with using hard-coded 30,390 if you print this
        #x=30 and x=390 are both in the lowest 0.001% of the x axis
        #print x                                                                                                                            

        xlab=np.concatenate([np.arange(30,181,30),np.arange(-150,31,30)])
        plt.xticks(np.linspace(30,390,13),xlab)

        #Working version commented below
        #plt.xticks(np.linspace(min(x[0]),max(x[0]), len(xlab)),xlab)
        plt.tick_params(labelsize=8) 

plt.show()
Answered By: J Richard Snape
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.