Python 在 matplotlib 图中绘制平滑曲线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14705062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:12:37  来源:igfitidea点击:

Plotting a smooth curve in matplotlib graphs

pythonmatplotlibnetcdf

提问by

I am reading a netcdf file using python and need to plot a graphs using matplotlib library in python. The netcdf file is containing 3 variables: uvand wcomponents. I have to draw these 3 components on a vertical scale. Since these data is going to be used in a weather forecasting radar's. I need to plot these graphs with smooth curves around the corner. now the plot looks likeand the code is...

我正在使用 python 读取 netcdf 文件,需要在 python 中使用 matplotlib 库绘制图形。netcdf 文件包含 3 个变量:uvw组件。我必须在垂直比例上绘制这 3 个组件。由于这些数据将用于天气预报雷达。我需要用拐角处的平滑曲线绘制这些图。现在情节看起来喜欢和代码是......

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

采纳答案by Eric O Lebigot

A simple solution is to do interpolation with splines, as explained in a nice Scipy tutorial.

一个简单的解决方案是使用样条进行插值,如一个很好的 Scipy 教程中所述

Here is an example:

下面是一个例子:

enter image description here

在此处输入图片说明