问题描述
我正在使用Matplotlib创建轮廓图.我有所有数据在多维数组中.它是12长,大约2000宽.就是这样基本上是12个长度为2000的列表的列表.我有等高线图工作正常,但我需要使数据平滑.我读了很多例子.不幸的是,我没有数学背景来了解什么是跟他们继续.
I am working on creating a contour plot using Matplotlib. I have all of the datain an array that is multidimensional. It is 12 long about 2000 wide. So it isbasically a list of 12 lists that are 2000 in length. I have the contour plotworking fine, but I need to smooth the data. I have read a lot ofexamples. Unfortunately, I don't have the math background to understand what isgoing on with them.
那么,我该如何平滑这些数据?我有一个例子,说明我的图形是什么样的以及我希望它看起来像什么.
So, how can I smooth this data? I have an example of what my graph looks likeand what I want it to look more like.
这是我的图
我也希望它看起来更相似:
What I want it to look more similar too:
我必须像第二幅图中那样平滑轮廓图吗?
What means do I have to smooth the contour plot like in second plot?
我正在使用的数据是从XML文件中提取的.但是,我将显示数组的一部分.由于数组中每个元素的长度约为2000个项目,因此我只会显示摘录.
The data I am using is pulled from an XML file. But, I will show the output ofpart of the array. Since each element in the array is around 2000 items long, Iwill only show an excerpt.
以下是示例:
[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后,我可以查看这些值,并且它们可以改变.但是,这些变化不足以影响等高线图.
Keep in mind this is only an excerpt. The dimension of the data is 12 rows by1959 columns. The columns change depending on the data imported from the XMLfile. I can look at the values after I use the Gaussian_filter and they dochange. But, the changes are not great enough to affect the contour plot.
推荐答案
您可以使用 gaussian_filter :
import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage as ndimage
X, Y = np.mgrid[-70:70, -70:70]
Z = np.cos((X**2+Y**2)/200.)+ np.random.normal(size=X.shape)
# Increase the value of sigma to increase the amount of blurring.
# order=0 means gaussian kernel
Z2 = ndimage.gaussian_filter(Z, sigma=1.0, order=0)
fig=plt.figure()
ax=fig.add_subplot(1,2,1)
ax.imshow(Z)
ax=fig.add_subplot(1,2,2)
ax.imshow(Z2)
plt.show()
左侧显示原始数据,高斯滤波后的右侧显示
The left-side shows the original data, the right-side after gaussian filtering.
以上大部分代码摘自 Scipy Cookbook ,该书演示了使用手工制作的高斯核.由于scipy具有相同的内置功能,因此我选择使用gaussian_filter
.
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
.
这篇关于使用Matplotlib平滑轮廓图中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!