0

I receive "ValueError: setting an array element with a sequence" when running. I have tried to turn everything into a numpy array to no avail.

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)

EDIT I sort of solved this problem by removing values from my sub-arrays in order to make the lengths equal, however I would still like to know how it is possible to feed an array containing sub-arrays of different lengths into contour plot.

Cam K
  • 127
  • 2
  • 2
  • 13
  • 1
    Possible duplicate of [ValueError: setting an array element with a sequence](https://stackoverflow.com/questions/4674473/valueerror-setting-an-array-element-with-a-sequence) – cookiedough Mar 18 '19 at 20:41
  • Your array is jagged which cannot be converted into NumPy array. All the subarrays have to be of same size to convert them to NumPy array. – Sheldore Mar 18 '19 at 20:45
  • So I should keep everything in list form? – Cam K Mar 18 '19 at 20:50
  • What kind of input does contour want? – Cam K Mar 18 '19 at 21:20
  • Contour wants x, y, z, where either x, y, and z are two-dimensional arrays of the same shape, or x and y are one-dimensional arrays and z has shape (len(y), len(x)). Note that the first dimension of z is len(y) and not len(x). – Mark Bakker Mar 18 '19 at 21:53
  • I made each subarray have the same length using zeros. I get a contour graph with the correct data but with loads of lines across it that I don't want (facing [0,0]). Any ideas how to make the subarrays the same length without using zeros? – Cam K Mar 18 '19 at 21:56
  • 1
    The grid (`x`, `y`) needs to be rectilinear for `contour` to work, i.e. values must be sorted and dimensions need to match. You can either interpolate your values to such a grid, or you can use `tricontour` where this restriction does not hold. – ImportanceOfBeingErnest Mar 19 '19 at 01:12

1 Answers1

0

The answer is to make X, Y and Z inputs all 1D arrays and to use tricontour instead of contour.

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()
Cam K
  • 127
  • 2
  • 2
  • 13