I am having a bit of trouble making a general plotting function that plots the real and imaginary parts of a function that is provided. I want to generalise my function so that it can accept an arbitrary number of function inputs and then plot them with a legend. Hence I should be able to call the function using:
tester(0.9, func1)
tester(0.9, func1, func2)
tester(0.9, func1, func2, func3, …)
and the function responds accordingly. What is the most compact way of doing this? Furthermore if the legend could be placed outside the two subplots (since the labels apply to both plots) that would be preferred.
At the moment I just have the following for manually plotting two two functions:
import numpy as np
import matplotlib.pyplot as plt
def tester(radius, func1, func2):
theta = np.linspace(0,2.1*np.pi,1000)
z = radius*np.exp(1j*theta)
w1 = func1(z)
w2 = func2(z)
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.plot(theta, np.real(w1), label='%s' % func1)
ax2.plot(theta, np.imag(w1))
ax1.plot(theta, np.real(w2), label='%s' % func2)
ax2.plot(theta, np.imag(w2))
ax1.legend()
ax1.set_ylabel('Real Component')
ax2.set_ylabel('Imag Component')
ax2.set_xlabel(r'Polar $\theta$ at $r=$%.2f' % radius)
plt.show()
return 0
def func1(z):
return np.sqrt(z)
def func2(z):
return np.sqrt(z**2-1)
tester(0.9, func1, func2)