I'm converting some simple octave/matlab programs into python, but in using matplotlib's contour
function, previously plotted 2D points all seem to align or flatten onto x=1. Any ideas why this is happening?
For a logistic regression exercise, in octave/matlab I plot some data points
plotData(X(:,2:3), y);
and then show the decision boundary around different categories. This last part is done with contour
:
contour(u, v, z, [0, 0], 'LineWidth', 2)
The result is this:
In python, everything goes well plotting points:
p1 = plt.plot(X[pos,0], X[pos,1], marker='+', markersize=9, color='k')[0]
p2 = plt.plot(X[neg,0], X[neg,1], marker='o', markersize=7, color='y')[0]
but when then immediately after using contour
as below, the first image is output:
p3 = plt.contour(u, v, z, levels=[0], linewidth=2)
As a note, u
and v
are identical in octave/matlab and python, both created with linspace(-1, 1.5, 50)
(python notation). I've even taken care manually to ensure that the python z
array is identical in python to the octave/matlab z
matrix to see if the values were affecting this behavior.
A few different google searches haven't yielded much and I haven't spotted anything in the python or matlab contour
documentation. Any pointers are greatly appreciated!
Update:
A few minutes after posting this, I found a solution but not a reason why this happens (so I'm keeping the question open).
Instead of calling
p1 = plt.plot(X[pos,0], X[pos,1], marker='+', markersize=9, color='k')[0]
p2 = plt.plot(X[neg,0], X[neg,1], marker='o', markersize=7, color='y')[0]
I called the following, a lot more like in the original octave/matlab script:
pd.plotData(X[:,1:3],y)
the plotData function contains the exact same previous two plt.plot(...)
lines. So, even though the code was identical, this healthier (less repetitive) way of plotting did the trick and the data points no longer align to x=1. Any thoughts on why this happens are welcome!