Python中matplotlib等高线图的ValueError

问题描述 投票:0回答:1

我在运行时收到“ValueError:设置带序列的数组元素”。我试图把一切变成一个numpy数组无济于事。

import matplotlib
import numpy as np
from matplotlib import pyplot

X=np.array([
    np.array([1,2,3,4,5,6,7]),
    np.array([1,2,3,4,5,6,7]),
    np.array([1,2,3,4,5,6,6.5,7.5]),
    np.array([1,2,3,4,5,6,7,8]),
    np.array([1,2,3,4,5,6,7,8,8.5]),
    np.array([1,2,3,4,5,6,7,8]),
    np.array([1,2,3,4,5,6,7])])

Y=np.array([
    np.array([1,1,1,1,1,1,1]),
    np.array([2,2,2,2,2,2,2]),
    np.array([3,3,3,3,3,3,2.5,3]),
    np.array([4,4,4,4,4,4,4,4]),
    np.array([5,5,5,5,5,5,5,5,5]),
    np.array([6,6,6,6,6,6,6,6]),
    np.array([7,7,7,7,7,7,7])])

Z=          np.array([
            np.array([4190, 4290, 4200, 4095, 4181, 4965, 4995]),
            np.array([4321, 4389, 4311, 4212, 4894, 4999, 5001]),
            np.array([4412, 4442, 4389, 4693, 4899, 5010, 5008, 4921]),
            np.array([4552, 4651, 4900, 4921, 4932, 5020, 4935, 4735]),
            np.array([4791, 4941, 4925, 5000, 4890, 4925, 4882, 4764, 4850]),
            np.array([4732, 4795, 4791, 4852, 4911, 4865, 4919, 4862]),
           np.array([4520, 4662, 4735,4794,4836,4852,4790])])

matplotlib.pyplot.contour(X, Y, Z)

编辑我通过从子数组中删除值来解决这个问题,以使长度相等,但我仍然想知道如何将包含不同长度的子数组的数组提供到等高线图中。

python-3.x numpy matplotlib contour valueerror
1个回答
0
投票

答案是使X,Y和Z输入所有1D阵列并使用tricontour而不是轮廓。

X=np.array([1,2,3,4,5,6,7,
        1,2,3,4,5,6,7,
        1,2,3,4,5,6,6.5,7.5,
        1,2,3,4,5,6,7,8,
        1,2,3,4,5,6,7,8,9,
        1,2,3,4,5,6,7,8,
        1,2,3,4,5,6,7])

Y=np.array([1,1,1,1,1,1,1,
2,2,2,2,2,2,2,
3,3,3,3,3,3,2.5,3,
4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7])

Z=     np.array([80, 73, 65, 57, 61, 55, 60,
        78, 73, 71, 55, 55, 60, 90,
        65, 62, 61, 61, 51, 60, 71, 78,
        70, 58, 58, 65, 80, 81, 90, 81,
        80, 59, 51, 58, 70, 70, 90, 89, 78,
        90, 63, 55, 58, 65, 78, 79, 70,
        100, 68, 54,52,60,72,71])

Y=np.flip(Y,0)

asdf=matplotlib.pyplot.tricontour(X, Y, Z,11)
matplotlib.pyplot.xlim([1,8])
matplotlib.pyplot.ylim([1,7])
matplotlib.pyplot.clabel(asdf, fontsize=6, inline=0)
matplotlib.pyplot.show()
© www.soinside.com 2019 - 2024. All rights reserved.