2

I would like to plot a series of contour lines, but the spacing between where the label is and the line increases higher up the page. I've plotted an example attached. I want no decimal points, hence used fmt, but this seems to change the spacing at different points (Ideally I want around half a centimetre gap between the contour line break and the writing.

As an aside, I also tried to use the manual locations so it'd plot each label at a certain place, but as there are two seperate contour lines with the same value I'm not sure if this is possible. Thanks!

Here is my code;

from netCDF4 import Dataset
import numpy as np
from matplotlib import pyplot as plt

#############################
#                           #
#       Parameter Setup     #
#                           #
#############################

    myfile = '/home/ubuntu/Documents/Control/puma/run/Control.nc'   #Control U
myfile2 =  '/home/ubuntu/Documents/Control_Trop_40K/puma/run/ControlTrop.nc'    #Perturbed U
Title = 'U'
Units = '!U'
Variable = 'ua'

#############################
#############################
Import = Dataset(myfile, mode='r') 
Import2 = Dataset(myfile2, more='r')

latt = Import.variables['lat'][:]
level = Import.variables['lev'][:] 
UControl = Import.variables[Variable][:]
#UPerturbed = Import2.variables[Variable][:]


#UChange = UPerturbed - UControl
#UChange = np.mean(UChange[:,:,:,0], axis=0)
UControl = np.mean(UControl[:,:,:,0], axis=0)


Contourrange = [10]
CS=plt.contour(latt,level,UControl,Contourrange, colors='k')
plt.clabel(CS, fontsize=10, inline=1,fmt = '%1.0f',ticks=Contourrange)



plt.gca().invert_yaxis()
plt.yscale('log', nonposy='clip')
plt.xticks(np.round((np.arange(-90, 91, 30)), 2))
plt.xlim(xmin=-90)
plt.yticks([900,800,700,600,500,400,300,200,100,50])
plt.gca().set_yticklabels([900,800,700,600,500,400,300,200,100,50])


plt.xlabel('Latitude')        
plt.ylabel('Pressure (hPa)')    
plt.title(Title)
plt.show()

The pictures are: enter image description here enter image description here

4
  • Edit = I have discovered inline_spacing, which helps but it still seems more seperated higher up the chart (possibly because its a log axis)
    – J W
    Jan 24, 2015 at 14:53
  • Have you looked at the inline_spacing argument to plt.clabel? I think this controls what you're trying to do.
    – farenorth
    Jan 24, 2015 at 14:58
  • I manually set this, but it doesn't seem to be setting the right amount of pixels either side consistently - values near the top are still much more spaced out
    – J W
    Jan 24, 2015 at 15:04
  • With this parameter set to 6, and adding more lines, it still spaces the ones further up more onedrive.live.com/redir?resid=4A445014A6259A95%2149415
    – J W
    Jan 24, 2015 at 15:07

1 Answer 1

2

You are manually defining the values for which it should be a tick:

plt.yticks([900,800,700,600,500,400,300,200,100,50])  

Since you also have chosen a logarithmic scale, and since the increment you specified is constant, matplotlib needs to vary the space between ticks to comply with both your requirements.

If you absolutely do not want this behavior, either get rid of the log option, or let matplotlib automatically set ticks for you. Alternatively, you could provide the plt.yticks fuction with an array of exponentially increasing/decreasing numbers. Like this:

plt.yticks([10^3,10^2,10^1])  

You will have to make sure you are using the correct base (I simply assumed a base 10), and you will have to find suitable numbers to span your range of values.

2
  • 2
    @James Warner, I could not run your code because it relies on data that you do not provide. Improve your question to include stand alone data and I will try to improve my answer and tests its appropriateness. Jan 25, 2015 at 23:27
  • 2
    Also, I see that you have another question that is simply an edit of this one. You should remove it before someone flags it as duplicate. Your comments above already reflects the states of your progresses. In the future consider editing/commenting your question rather than writing a new one. Jan 25, 2015 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.