I've quickly skimmed old questions but didn't find this specific one. Sorry if it's a duplicate!
I'm trying to plot a secondary axis, with ax.secondary_yaxis(), in which the function that connects the two axes involves an additional variable. This is my example:
x = np.linspace(0, 50, 100)
y = np.linspace(220, 273, 100)
k = np.linspace(-10, 10, 100)
def y_to_y2(y):
return y * k
def y2_to_y(y2):
return y2 / k
fig, ax = plt.subplots()
ax2 = ax.secondary_yaxis("right", functions=(y_to_y2, y2_to_y))
ax.plot(x, y)
But I get this error:
ValueError: operands could not be broadcast together with shapes (2,) (100,)
Thanks a lot for your help and suggestions!

y_to_y2is not the same y as defined in the beginning of your code. Try to add a print(y) statement inside the function. It is a list of size two, hence the error. Have a look at the examples in the matplotlib secondary_yaxis docs to understand what transformations this can do.