Contour labels in Python
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.
J W
Updated on June 04, 2022Comments
-
J W almost 2 years
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:
-
snake_charmer over 9 years@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.
-
snake_charmer over 9 yearsAlso, 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.