1

I am trying to contour plot this equation (z) but complier gives me "TypeError: Input z must be a 2D array." error I have tried different methods but it still gives the same error

import matplotlib

import numpy as np

import matplotlib.cm as cm

import matplotlib.pyplot as plt

import math

fig = plt.figure()

ax = fig.add_subplot(111)

a = np.linspace(-6,6,100)

X , Y = np.meshgrid(a,a)

v = 1

a = 1

g1 = 2 * math.pi

g2 = -g1

z =  v * y - v *y *(a**2 / (x**2 + (y- 2*a )**2))  + (g1 / 4 * (math.pi)*(np.log((x**2 +(y-2*a)**2) / a))) -  v * y *(a**2 /(x**2+(y+2*a)**2)) + (g2 / 4*(math.pi)*(np.log((x**2+(y+2*a)**2))) / a)

ax.contourf(X,Y,z)
Christian K.
  • 2,785
  • 18
  • 40
  • 1
    This: [Why does pyplot.contour() require Z to be a 2D array?](https://stackoverflow.com/questions/42045921/why-does-pyplot-contour-require-z-to-be-a-2d-array) – ImportanceOfBeingErnest Jan 27 '20 at 18:45
  • 2
    Your code above doesn't actually work if it is copied and pasted. Try replacing `X` and `Y` with `x` and `y` or vice-versa. Does that give you the plot you want? – tomjn Jan 27 '20 at 18:47

1 Answers1

1

Renaming all upper case X and Y to lower case x and y creates this picture:

enter image description here

You probably work in a Notebook and use other values for x and y, i.e. scalars.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161