使用MatPlotLib基于z值矩阵生成等高线图

时间:2014-05-04 23:27:44

标签: python matplotlib

说我有一份清单字典:

dict = {}
for x in range(0, 11):
    dict[x] = [0,1,2,3,4,5,6,7,8,9,10]

如何从这些数据创建等高线图?数据结构基本上是z值的矩阵,其中x和y坐标分别等于字典键和值。

1 个答案:

答案 0 :(得分:1)

如果我已正确理解您的数据类型,要将其转换为numpy数组然后绘制它,您可以执行以下操作:

import numpy as np
import pylab as plt

# The example dict/matrix
dict = {}
for x in range(0, 11):
    dict[x] = [0,1,2,3,4,5,6,7,8,9,10]

# Create an empty numpy array with the right dimensions
nparr = np.zeros((len(dict.keys()), len(dict[0])))

# Loop through converting each list into a row of the new array
for ii in xrange(nparr.shape[0]):
    nparr[ii] = dict[ii]

# Plotting as a contour
plt.contour(nparr)
plt.show()

请注意,对于非常大的数据集,for循环不会特别快,但对于“图像大小”数据应该没问题(我希望matplotlib的渲染能够以最快的速度占用大部分时间)。

相关问题