# Understanding the method for plotting a pivot table from pandas

Alright,

I spent forever trying to get the ellipses on my graph oriented correctly and I finally have the answer correct but the coding doesn't make much sense to me... Let me give a though explanation with outputs.

Here's solution for plotting a contour graph...

def plot(data, xvar, yvar, zvar):
piv = data.pivot(xvar, yvar, zvar)
x = piv.columns.values
y = piv.index.values
X, Y = np.meshgrid(x,y)
Z = piv.values
plt.contourf(Y,X,Z, levels=50, alpha=3/4, cmap=plt.cm.jet)
plt.title(r"$C(\theta_1, \theta_2) = \sum_{i=1}^{20}[f(t_i)-\theta_1 t_i - \theta_2 t_i^2]^2$")
plt.xlabel(r"$\theta_1$")
plt.ylabel(r"$\theta_2$")


The docs for the pivot tables says that the first parameter is the index (x-var) and the second parameter is for any columns (y-var) and then 3rd parameter passed is the values for the pivot table (z-var).

Alright so

1. Why is x = piv.columns.values? we're assigning the results we want to show up on the y axis to the x axis and then for the x axis to the y axis... It's strange...

2. when we plot contourf we then do (Y,X,Z), this again seems counter intuitive to the (X,Y,Z) convention. Indeed, the docs say it should be the other way around.

From how I intuitively think about it, this is what ought to make the right graph

def plot(data, xvar, yvar, zvar):
piv = data.pivot(xvar, yvar, zvar)
x = piv.index.values
y = piv.columns.values
X, Y = np.meshgrid(x,y)
Z = piv.values
plt.contourf(X,Y,Z, levels=50, alpha=3/4, cmap=plt.cm.jet)
plt.title(r"$C(\theta_1, \theta_2) = \sum_{i=1}^{20}[f(t_i)-\theta_1 t_i - \theta_2 t_i^2]^2$")
plt.xlabel(r"$\theta_1$")
plt.ylabel(r"$\theta_2$")


Here's the correct graph that the first one makes...

Here's the incorrect graph that the second one makes...

What's an intuitive way to understand what's happening?