0

I have a two-variables function as

Z = m_1 m_2 / pow(m_1 + m_2, 2)

I want to plot the curves with constant values for Z in the m_1 - m_2 plane (contours). For example, Z=0.10, 0.15, 0.20, 0.25. What is the easiest way to do this job in python via matplotlib? Following is what I've tried but it doesn't quite what I want. I expect different results. I need to make sure if this is correct.

def etta(m_1, m_2):
    return m_1*m_2 / pow(m_1 + m_2, 2)

m_1 = linspace(4, 14, 100)
m_2 = linspace(4, 14, 100)

X,Y = meshgrid(m_1, m_2)

Z = etta(X, Y).T

print(sqrt(2))

fig, ax = plt.subplots()
cnt = contour(Z, cmap=cm.RdBu)

plt.show()
3
  • This seems like a pretty standard contour problem (isn't it?). Do you have any code that we could take a look at to see what isn't working? Commented Nov 22, 2013 at 20:54
  • I added my code. Thanks for looking! Commented Nov 22, 2013 at 21:05
  • Your code produces what I would have expected. You can also visualize it with `imshow(Z, origin='lower') to compare. What did you expect it to look like? Commented Nov 22, 2013 at 21:25

1 Answer 1

2

Perhaps this is what you want:

V = [0.10, 0.15, 0.20, 0.25]
cnt = plt.contour(X, Y, Z, V, cmap=cm.RdBu)

Which will draw lines at values given by V. The problem though, is that the values you gave mostly don't show up in the domain given by X and Y. You can see this by looking at the full function with imshow:

plt.imshow(Z, extent=(X.min(), X.max(), Y.min(), Y.max()), origin='lower', cmap=cm.RdBu)

imshow

contour

Increase the values in V to see more lines:

V = np.linspace(.1, .3, 25)
plt.contour(X,Y,Z,V, cmap=cm.RdBu)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.