python - Python 中的轮廓标签

标签 python matplotlib contour

我想绘制一系列等高线,但标签所在位置和线条之间的间距在页面上方增加。我已经绘制了一个附加的例子。我不需要小数点,因此使用了 fmt,但这似乎改变了不同点的间距(理想情况下,我希望轮廓线中断和书写之间有大约半厘米的间隙。

顺便说一句,我还尝试使用手动位置,以便将每个标签绘制在某个位置,但由于有两条具有相同值的独立轮廓线,我不确定这是否可能。谢谢!

这是我的代码;

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()

图片是: enter image description here enter image description here

最佳答案

您正在手动定义应为其打勾的值:

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

由于您还选择了对数刻度,并且指定的增量是恒定的,因此 matplotlib 需要改变刻度之间的间距以满足您的要求。

如果您绝对不希望出现这种行为,请删除日志选项,或者让matplotlib自动为您设置刻度。或者,您可以为 plt.yticks 函数提供一组按指数递增/递减的数字。像这样:

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

您必须确保使用正确的基数(我只是假设基数为 10),并且您必须找到合适的数字来跨越您的值范围。

关于python - Python 中的轮廓标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28126678/

相关文章:

python - Elastic Search Function_Score Query with Query_String

python - 如何使用 `matplotlib.pyplot.specgram` 绘制频带

r - 在 R 中与轮廓和多边形相交

matlab - MATLAB 中使用 Geoshow 绘制颜色图

python - 在没有 split() 的情况下在 Python 中拆分字符串

Python serial - 尝试使用未打开的端口

python - 给定改变属性的函数计算对象属性的 "closure"

python - 如何在 gtk3 中创建一个实时更新的 matplotlib 图

Python绘图图像保存错误

c++ - 查找轮廓 : Measure distance between contours