我正在使用 Matplotlib 创建等高线图。我有所有数据
在多维数组中。长12米,宽约2000米。原来如此
基本上是一个由 12 个列表组成的列表,长度为 2000。我有等高线图
工作正常,但我需要平滑数据。我读过很多
例子。不幸的是,我没有数学背景来理解什么是
和他们一起继续下去。
那么,如何平滑这些数据呢?我有一个图表的示例
以及我希望它看起来更像什么。
这是我的图表:
我希望它看起来也更相似:
我必须用什么方法才能像第二个图中那样平滑等值线图?
我使用的数据是从 XML 文件中提取的。但是,我将显示的输出
数组的一部分。由于数组中的每个元素大约有 2000 个项目长,我
只会显示摘录。
这是一个示例:
[27.899999999999999, 27.899999999999999, 27.899999999999999, 27.899999999999999,
28.0, 27.899999999999999, 27.899999999999999, 28.100000000000001, 28.100000000000001,
28.100000000000001, 28.100000000000001, 28.100000000000001, 28.100000000000001,
28.100000000000001, 28.100000000000001, 28.0, 28.100000000000001, 28.100000000000001,
28.0, 28.100000000000001, 28.100000000000001, 28.100000000000001, 28.100000000000001,
28.100000000000001, 28.100000000000001, 28.100000000000001, 28.100000000000001,
28.100000000000001, 28.100000000000001, 28.100000000000001, 28.100000000000001,
28.100000000000001, 28.100000000000001, 28.100000000000001, 28.100000000000001,
28.100000000000001, 28.100000000000001, 28.0, 27.899999999999999, 28.0,
27.899999999999999, 27.800000000000001, 27.899999999999999, 27.800000000000001,
27.800000000000001, 27.800000000000001, 27.899999999999999, 27.899999999999999, 28.0,
27.800000000000001, 27.800000000000001, 27.800000000000001, 27.899999999999999,
27.899999999999999, 27.899999999999999, 27.899999999999999, 28.0, 28.0, 28.0, 28.0,
28.0, 28.0, 28.0, 28.0, 27.899999999999999, 28.0, 28.0, 28.0, 28.0, 28.0,
28.100000000000001, 28.0, 28.0, 28.100000000000001, 28.199999999999999,
28.300000000000001, 28.300000000000001, 28.300000000000001, 28.300000000000001,
28.300000000000001, 28.399999999999999, 28.300000000000001, 28.300000000000001,
28.300000000000001, 28.300000000000001, 28.300000000000001, 28.300000000000001,
28.399999999999999, 28.399999999999999, 28.399999999999999, 28.399999999999999,
28.399999999999999, 28.300000000000001, 28.399999999999999, 28.5, 28.399999999999999,
28.399999999999999, 28.399999999999999, 28.399999999999999]
请记住,这只是摘录。数据的维度为 12 行
1959年专栏。列根据从 XML 导入的数据而变化
文件。我可以在使用 Gaussian_filter 后查看这些值,它们确实如此
改变。但是,变化不足以影响等值线图。
I am working on creating a contour plot using Matplotlib. I have all of the data
in an array that is multidimensional. It is 12 long about 2000 wide. So it is
basically a list of 12 lists that are 2000 in length. I have the contour plot
working fine, but I need to smooth the data. I have read a lot of
examples. Unfortunately, I don't have the math background to understand what is
going on with them.
So, how can I smooth this data? I have an example of what my graph looks like
and what I want it to look more like.
This is my graph:
What I want it to look more similar too:
What means do I have to smooth the contour plot like in second plot?
The data I am using is pulled from an XML file. But, I will show the output of
part of the array. Since each element in the array is around 2000 items long, I
will only show an excerpt.
Here is a sample:
[27.899999999999999, 27.899999999999999, 27.899999999999999, 27.899999999999999,
28.0, 27.899999999999999, 27.899999999999999, 28.100000000000001, 28.100000000000001,
28.100000000000001, 28.100000000000001, 28.100000000000001, 28.100000000000001,
28.100000000000001, 28.100000000000001, 28.0, 28.100000000000001, 28.100000000000001,
28.0, 28.100000000000001, 28.100000000000001, 28.100000000000001, 28.100000000000001,
28.100000000000001, 28.100000000000001, 28.100000000000001, 28.100000000000001,
28.100000000000001, 28.100000000000001, 28.100000000000001, 28.100000000000001,
28.100000000000001, 28.100000000000001, 28.100000000000001, 28.100000000000001,
28.100000000000001, 28.100000000000001, 28.0, 27.899999999999999, 28.0,
27.899999999999999, 27.800000000000001, 27.899999999999999, 27.800000000000001,
27.800000000000001, 27.800000000000001, 27.899999999999999, 27.899999999999999, 28.0,
27.800000000000001, 27.800000000000001, 27.800000000000001, 27.899999999999999,
27.899999999999999, 27.899999999999999, 27.899999999999999, 28.0, 28.0, 28.0, 28.0,
28.0, 28.0, 28.0, 28.0, 27.899999999999999, 28.0, 28.0, 28.0, 28.0, 28.0,
28.100000000000001, 28.0, 28.0, 28.100000000000001, 28.199999999999999,
28.300000000000001, 28.300000000000001, 28.300000000000001, 28.300000000000001,
28.300000000000001, 28.399999999999999, 28.300000000000001, 28.300000000000001,
28.300000000000001, 28.300000000000001, 28.300000000000001, 28.300000000000001,
28.399999999999999, 28.399999999999999, 28.399999999999999, 28.399999999999999,
28.399999999999999, 28.300000000000001, 28.399999999999999, 28.5, 28.399999999999999,
28.399999999999999, 28.399999999999999, 28.399999999999999]
Keep in mind this is only an excerpt. The dimension of the data is 12 rows by
1959 columns. The columns change depending on the data imported from the XML
file. I can look at the values after I use the Gaussian_filter and they do
change. But, the changes are not great enough to affect the contour plot.
发布评论
评论(2)
您可以使用 :
"noreferrer"> gaussian_filter 左侧为原始数据,右侧为高斯滤波后的数据。
上面的大部分代码取自 Scipy Cookbook,它演示了使用手工制作的高斯平滑高斯核。由于 scipy 具有相同的内置功能,因此我选择使用 gaussian_filter 。
You could smooth your data with a gaussian_filter:
The left-side shows the original data, the right-side after gaussian filtering.
Much of the above code was taken from the Scipy Cookbook, which demonstrates gaussian smoothing using a hand-made gauss kernel. Since scipy comes with the same built in, I chose to use
gaussian_filter
.平滑数据的一种简单方法是使用移动平均算法。移动平均值的一种简单形式是计算某个位置的相邻测量值的平均值。例如,在一系列一维测量值 a[1:N] 中,a[n] 处的移动平均值可计算为 a[n] = (a[n-1] + a[n] + a[例如,n+1])/3。如果您完成了所有测量,那么您就完成了。在这个简单的示例中,我们的平均窗口大小为 3。您还可以使用不同大小的窗口,具体取决于您想要的平滑程度。
为了使计算更容易、更快速,适用于更广泛的应用,您还可以使用基于卷积的算法。使用卷积的优点是,您可以通过简单地更改窗口来选择不同类型的平均值,例如加权平均值。
让我们做一些编码来说明。以下摘录需要安装 Numpy、Matplotlib 和 Scipy。 单击此处查看完整的运行示例代码
以下代码生成一些任意且嘈杂的数据然后使用四个不同大小的框窗口计算移动平均值。
然后,为了查看不同的结果,这里是一些绘图的代码。
以下是不同大小窗口的绘制结果:
此处给出的示例代码使用简单的框(或矩形)窗口在二维中。有几种不同类型的窗口可用,您可能需要查看维基百科了解更多示例。
One easy way to smooth data is using a moving average algorithm. One simple form of moving average is to calculate the average of adjacent measurements at a certain position. In a one-dimensional series of measurements a[1:N], for example, the moving average at a[n] can be calculated as a[n] = (a[n-1] + a[n] + a[n+1]) / 3, for example. If you go through all of your measurements, you're done. In this simple example, our averaging window has size 3. You can also use windows of different sizes, depending on how much smoothing you want.
To make the calculations easier and faster for a wider range of applications, you can also use an algorithm based on convolution. The advantage of using convolution is that you can choose different kinds of averages, like weighted averages, by simply changing the window.
Let's do some coding to illustrate. The following excerpt needs Numpy, Matplotlib and Scipy installed. Click here for the full running sample code
The following code generates some arbitrary and noisy data and then calculates the moving average using four different sized box windows.
And then, to see the different results, here is the code for some plotting.
And here are the plotted results for different sized windows:
The sample code given here uses a simple box (or rectangular) window in two dimensions. There are several different kinds of windows available and you might want to check Wikipedia for more examples.