0
votes

I have a function which returns a tuple of x and y data, in the form

optical():
    xvals = [0,1]
    yvals = [0,1]

    return xvals, yvals

I can modify the return value of this function, but ultimately I need to be able to do

import matplotlib.pyplot as plt

plt.plot(optical())
plt.show()

And get a plot of xvals and yvals.

The behavior I get with the above setup is four points (at (0,0), (1,0), (0,1), and (1,1)) instead of two (at (0,0) and (1,1)).

I am explicitly avoiding

import matplotlib.pyplot as plt

x, y = optical()
plt.plot(x,y)
plt.show()

Is what I want to do possible, and if so, how can it be accomplished?

1

1 Answers

0
votes

You can unpack x and y from optical directly in your call to plt.plot using the * operator:

plt.plot(*optical())
plt.show()

The use of the * operator in this case is is called iterable unpacking, you can read about it here