如何在 PyPlot 图中创建平滑线?
在 PyPlot 中绘制平滑线
问题:
使用 PyPlot 绘制图形时,数据点之间的连接线可能会显得僵硬且不连续。在某些情况下,这可能是不可取的。
问题:
如何平滑 PyPlot 图中的连接线?
解决方案:
为了获得更平滑的线条,可以利用 scipy 的样条插值技术。具体方法如下:
<code class="python">import matplotlib.pyplot as plt import numpy as np import scipy.interpolate T = np.array([6, 7, 8, 9, 10, 11, 12]) power = np.array([1.53E+03, 5.92E+02, 2.04E+02, 7.24E+01, 2.72E+01, 1.10E+01, 4.70E+00]) # Create a dense array of points for interpolation xnew = np.linspace(T.min(), T.max(), 300) # Interpolate the data using a cubic spline power_smooth = scipy.interpolate.spline(T, power, xnew) # Plot the smoothed line plt.plot(xnew, power_smooth) plt.show()</code>
注意: scipy 中的 'spline' 函数在 0.19.0 版本中已弃用。请改用“BSpline”类。这是更新版本:
<code class="python">from scipy.interpolate import make_interp_spline, BSpline # Create a dense array of points for interpolation xnew = np.linspace(T.min(), T.max(), 300) # Create a B-spline interpolation object spl = make_interp_spline(T, power, k=3) # type: BSpline # Evaluate the interpolation at the new points power_smooth = spl(xnew) # Plot the smoothed line plt.plot(xnew, power_smooth) plt.show()</code>
“make_interp_spline”中的“k”参数控制样条线的平滑度。 “k”值越高,线条越平滑。
生成的图将在数据点之间呈现平滑的连接线,从而提供更具视觉吸引力的数据表示。
以上是如何在 PyPlot 图中创建平滑线?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

攻克Investing.com的反爬虫策略许多人尝试爬取Investing.com(https://cn.investing.com/news/latest-news)的新闻数据时,常常�...

Python3.6环境下加载pickle文件报错:ModuleNotFoundError:Nomodulenamed...

使用Scapy爬虫时管道文件无法写入的原因探讨在学习和使用Scapy爬虫进行数据持久化存储时,可能会遇到管道文�...
