2
votes

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 hereenter image description here

1
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
Have you looked at the inline_spacing argument to plt.clabel? I think this controls what you're trying to do. - farenorth
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
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

1 Answers

2
votes

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.