4

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?

matplotlib's contour "pushes" dots to x=1...

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:

octave/matlab's proper contour result

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]

properly plotted data points

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!

data points properly plotted

arturomp
  • 28,790
  • 10
  • 43
  • 72
  • I'd guess it's a mistake in the plotting process that leads to weird results. Can you add a link to the data and code you use? The only possible mistake I see from you question is `u` and `v` should probably be from `np.meshgrid(x, y)` in contour. There are examples of contour/scatter (e.g. http://stackoverflow.com/questions/17431441/matplotlib-scatter-plot-to-foreground-on-top-of-a-contour-plot) which may be a good starting point (to see if bug in your version of `matplotlib` and build up to your example. – Ed Smith Sep 14 '15 at 15:53
  • thanks @EdSmith! I updated the question since now I get the correct behavior changing the code a bit, but I'm still not positive why. You can take a look at the code at http://git.io/vZyso – arturomp Sep 14 '15 at 17:27
  • 1
    It is hard to test without sample data. – TheBlackCat Sep 16 '15 at 11:59
  • @TheBlackCat all the code and data is in my previous comment! – arturomp Sep 16 '15 at 18:34
  • The implicit usage of objects might be an issue here. I recommend getting used to the axes object in python. You can create an axes explicitly using `fig = mp.figure()` and `ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])` and plot your data directly into the new axis `ax.plot(...)` and `ax.contour(...)`. – wsj Nov 06 '15 at 13:44

0 Answers0