setting an array element with a sequence matplolib 3d
setting an array element with a sequence matplolib 3d
I have the following strange problem. I am trying to do a 3d plot. That works ok. I wanted to put the projections on the surfaces of the plot. My code looks at the moment like this
fig = plt.figure(figsize = (10,8))
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(xarr, yarr, zarr, cmap=cm.coolwarm, linewidth=50)
ax.set_xlabel('nMAE', fontsize = 14, linespacing = 1.5)
ax.set_ylabel('nDIFF', fontsize = 14)
ax.set_zlabel('nCounts', fontsize = 14, linespacing=1.5)
cset = ax.contour(np.array(xx), np.array(yy),
np.array(zz), zdir='z', offset=-100, cmap=cm.coolwarm)
cset = ax.contour(xx, yy, np.array(zz), zdir='x', offset=-40, cmap=cm.coolwarm)
cset = ax.contour(xx, yy, np.array(zz), zdir='y', offset=40, cmap=cm.coolwarm)
plt.show()
What is not working is the following line
cset = ax.contour(np.array(xx), np.array(yy),
np.array(zz), zdir='z', offset=-100, cmap=cm.coolwarm)
Here are the vectors
np.array(yy)
array([ 21, 6, 30, 3, 27, 61, 56, 52, 38, 14, 33, 12, 93,
129, 36, 11, 59, 9, 113, 18, 26, 8, 17, 10, 29, 2,
4, 16, 85, 55, 58, 45, 7, 15, 19, 5, 69, 57, 20,
158, 86, 118, 31, 107, 34, 92, 32, 28, 66, 54, 87, 25,
13, 99, 23, 60, 81, 24, 72, 123, 49, 63, 64, 71, 67,
40, 46, 48, 47, 95, 43, 159, 22, 37, 35, 105, 104, 42,
128, 53, 76, 75, 103, 65, 136, 144, 68, 77, 278, 98, 111,
114, 41, 84, 154, 62, 214, 124, 210, 1, 155, 79, 74, 80,
83, 318, 70, 120, 78, 44, 88, 73, 50, 110, 178, 51, 134,
106, 189, 91, 411, 135, 138, 143, 127, 122, 160, 94, 109, 226,
140, 117, 100, 133, 191, 141, 89, 288, 126, 97, 653, 121, 172,
161, 39, 96, 90, 130, 169, 142, 82, 132, 156, 137, 119, 102,
112, 188, 610, 115, 146, 234, 108, 150, 182, 170, 116, 223, 139,
197, 194, 241, 131, 181, 183, 152, 147, 250, 203, 165, 199, 218,
334, 167, 151, 384, 163, 162, 125, 148, 233, 354, 184, 168, 186,
180, 166, 369, 192, 101, 201, 157, 164, 419, 239], dtype=int64)
and
np.array(xx)
array([ 500., 1500., 2500., 3500., 4500., 5500., 6500.,
7500., 8500., 9500., 10500., 11500., 12500., 13500.,
14500., 15500., 16500., 17500., 18500., 19500., 20500.,
21500., 22500., 23500., 24500., 25500., 26500., 27500.,
28500.])
the zz has dimensions
np.array(zz).shape
(205,29)
as it should. Anyone can guess what is wrong? The complete error is
ValueError: setting an array element with a sequence.
I cannot unfortunately publish the data, but I hope the error is linked to how the data are structured...
Thanks in advance, Umberto
1 Answer
1
If you check shapes of X
, Y
and Z
in contour3d example, you shall
find out that they are the same.
So, in order to make your code working, you
should extend your xx
and yy
to 2d arrays with np.meshgrid
before creating a plot.
X
Y
Z
xx
yy
np.meshgrid
xx, yy = np.meshgrid(xx, yy)
Glad to be helpful!
– taras
Jun 29 at 12:25
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
My mistake. You are right... Thanks.
– Umberto
Jun 29 at 11:34