python 等值线 标注 间距、控制_python - Matplotlib-Contourf-如何使刻度线间距不均匀? - 堆栈内存溢出...
根據(jù)yticks上的matplotlib文檔,您可以指定要使用的標簽。 在您的情況下,如果要顯示標簽[10,100,500,1000,1500,2000,3000,4000,5000] ,則可以將該列表作為第二個參數(shù)傳遞給plt.yticks() ,就像這樣
plt.yticks(z[pos], [10,100,500,1000,1500,2000,3000,4000,5000])
并會相應顯示yticks 。 問題出現(xiàn)在位置的指定中-由于depth數(shù)組沒有與所需ytick值完全對應的點,因此您需要進行插值才能找到放置標簽的確切位置。 除非在pos中指定的大概位置已經(jīng)足夠,否則在這種情況下就足夠了。
如果depth數(shù)據(jù)的間距不均勻,則可以使用numpy.interp進行插值,如下所示
import matplotlib.pyplot as plt
import numpy as np
# Create some depth data that is not uniformly spaced over [0, 5500]
depth = [(np.random.random() - 0.5)*25 + ii for ii in np.linspace(0, 5500, 50)]
lat = np.linspace(-75, 75, 50)
z = np.linspace(0,50, 50)
yticks = [10,100,500,1000,1500,2000,3000,4000,5000]
# Interpolate depths to get z-positions
pos = np.interp(yticks, depth, z)
temp = np.outer(lat, z) # Arbitrarily populate temp for demonstration
ax = plt.contourf(lat,z,temp)
plt.colorbar()
plt.gca().yaxis.set_ticks(pos)
plt.yticks(pos,yticks) # Place yticks at interpolated z-positions
plt.gca().invert_yaxis()
plt.grid(linestyle=':')
plt.gca().set(ylabel='Depth (m)',xlabel='Latitude')
plt.show()
如果depth數(shù)組在這些位置處有數(shù)據(jù),則將找到y(tǒng)ticks 將落入的確切位置,并將其相應放置,如下所示。
總結(jié)
以上是生活随笔為你收集整理的python 等值线 标注 间距、控制_python - Matplotlib-Contourf-如何使刻度线间距不均匀? - 堆栈内存溢出...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3spark文本分类_如何用
- 下一篇: 判断是不是素数python_Python