I have a strange looking function that calls the plots based on the attributes. So, if a function exists in the class then select that. Then I am trying to call it, in this example I use pyplot.arrow
, however, I cannot seem to unpack all the values. It should take four parameters, but I get the following error:
ValueError: too many values to unpack (expected 2)
I cannot recognise how I am passing too many values given I unpack with *
, here is what I have tried:
import numpy as np
import matplotlib.pyplot as plt
test_array = np.array([ [1, 2] , [5, 4] , [5, 2] , [1, 2] , [5, 2] , [1, 2]])
test_array = np.column_stack((test_array,[ [ 0 , 0 ] ]*test_array.shape[0]))
test_split = np.split(test_array, 6)
for b in test_split:
b[ : , [ 0 , 1 , -2 , -1 ] ] = b[ : , [ -2 , -1 , 0 ,1] ]
def plot(size: list,plType, *args, **kwargs):
figs, axs = plt.subplots(size[0], size[1], figsize=(8,8))
xy = np.array(args)
for A , ax in zip( xy , axs.flat ):
X = np.hsplit( A , xy.shape[2] )
if isinstance(X, list):
for ind , Z in enumerate(zip(*X)):
ax.__getattribute__( plType )( *Z, **kwargs)
plt.show()
print(np.array(test_split).shape)
plot([2, 3], 'arrow', *test_split)
CodePudding user response:
I feel like you are doing several unnecessary things which has made it confusing.
The main point is you want to do ax.__getattribute__("arrow")(x, y, dx, dy, **kwargs)
. To keep it simple:
import numpy as np
import matplotlib.pyplot as plt
test_array = np.array([
[1, 2],
[5, 4],
[5, 2],
[1, 2],
[5, 2],
[1, 2]
])
test_array = np.column_stack((test_array, [[0, 0]] * test_array.shape[0]))
test_split = np.split(test_array, 6)
for b in test_split:
b[:, [0, 1, -2 , -1]] = b[:, [-2, -1, 0 ,1]]
Here, test_split
is a set of 6 arrays of size 4; assuming they represent (x, y, dx, dy).
[array([[0, 0, 1, 2]]),
array([[0, 0, 5, 4]]),
array([[0, 0, 5, 2]]),
array([[0, 0, 1, 2]]),
array([[0, 0, 5, 2]]),
array([[0, 0, 1, 2]])]
We can plot them in the subplots as follows:
def plot(size: list, plType, *args, **kwargs):
figs, axs = plt.subplots(size[0], size[1], figsize=(8, 8))
for A, ax in zip(args, axs.flat):
ax.__getattribute__(plType)(*A.ravel().tolist(), **kwargs)
plt.show()
CodePudding user response:
You were missing another level of obfuscation.
import numpy as np
import matplotlib.pyplot as plt
test_array = np.array([ [1, 2] , [5, 4] , [5, 2] , [1, 2] , [5, 2] , [1, 2]])
test_array = np.column_stack((test_array,[ [ 0 , 0 ] ]*test_array.shape[0]))
test_split = np.split(test_array, 6)
for b in test_split:
b[ : , [ 0 , 1 , -2 , -1 ] ] = b[ : , [ -2 , -1 , 0 ,1] ]
def plot(size: list,plType, *args, **kwargs):
figs, axs = plt.subplots(size[0], size[1], figsize=(8,8))
xy = np.array(args)
for A , ax in zip( xy , axs.flat ):
X = np.hsplit( A , xy.shape[2] )
if isinstance(X, list):
for i, Z in enumerate(zip(*X)_:
############################################################
for j, z in enumerate(zip(*Z)):
############################################################
ax.__getattribute__( plType )( *z, **kwargs)
plt.show()
print(np.array(test_split).shape)
plot([2, 3], 'arrow', *test_split)
I said obfuscation because, e.g., the enumerate
is completely unnecessary — but this is just one of many examples.