使用 Matplotlib 平滑轮廓图中的数据 [英] Smoothing Data in Contour Plot with Matplotlib

查看:61
本文介绍了使用 Matplotlib 平滑轮廓图中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Matplotlib 创建等高线图.我有所有的数据在多维数组中.它长12,宽约2000.所以是基本上是一个长度为 2000 的 12 个列表的列表.我有等高线图工作正常,但我需要平滑数据.我已经阅读了很多例子.不幸的是,我没有数学背景来理解什么是继续和他们在一起.

那么,我该如何平滑这些数据?我有一个例子说明我的图表是什么样的以及我希望它看起来更像什么.

这是我的图表:

我也希望它看起来更相似:

我必须像第二个图中那样平滑等高线图是什么意思?

<小时>

我使用的数据是从 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.10000000000001, 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 后查看这些值,它们确实如此改变.但是,变化不足以影响等高线图.

解决方案

您可以使用 ,它演示了使用手工制作的高斯内核.由于 scipy 内置相同,我选择使用 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.

解决方案

You could smooth your data with a 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.

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 平滑轮廓图中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆