在matplotlib图中绘制平滑曲线

13 投票
1 回答
47248 浏览
提问于 2025-04-17 14:48

我正在用Python读取一个netcdf文件,并需要使用matplotlib库来绘制图表。这个netcdf文件里包含了三个变量:uvw组件。我需要把这三个组件在一个垂直的坐标轴上画出来。因为这些数据是要用在天气预报雷达上的,所以我希望绘制的图形在转角的地方能比较平滑。现在的图看起来是这样的:like 而代码是...

from netCDF4 import Dataset
from pylab import *
import numpy  
from scipy import interpolate
from scipy.interpolate import spline


#passing the filename

root_grp=Dataset('C:\\Python27\\MyPrograms\\nnt206rwpuvw.nc')

#getting values of u component
temp1 = root_grp.variables['ucomponent']
data1 = temp1[:]
u=data1[0]

#getting values of v component
temp2 = root_grp.variables['wcomponent']
data2 = temp2[:]
v=data2[0]
#getting values of w component
temp3 = root_grp.variables['wcomponent']
data3 = temp3[:]
w=data3[0]
#creating a new array
array=0.15*numpy.arange(1,55).reshape(1,54)
#aliasing
y=array[0]
#sub-plots
f, (ax1, ax2, ax3) = plt.subplots(1,3, sharey=True)
ax1.plot(u,y,'r')
ax2.plot(v,y,'g')
ax3.plot(w,y,'b')
#texts
ax1.set_title('u component',fontsize=16,color='r')
ax2.set_title('v component',fontsize=16,color='g')
ax3.set_title('w component',fontsize=16,color='b')
show()

1 个回答

17

一个简单的解决办法是使用样条插值,具体的做法可以参考这个很不错的Scipy教程

下面是一个例子:

在这里输入图片描述

撰写回答