0

I am a recent migrant from Matlab to Python and have recently worked with Numpy and Matplotlib. I recoded one of my scripts from Matlab, which employs Matlab's contourf-function, into Python using matplotlib's corresponding contourf-function. I managed to replicate the output in Python, apart that the contourf-plots are not exacly the same, for a reason that is unknown to me. As I run the contourf-function in matplotlib, I get this otherwise nice figure but it has these sharp edges on the contour-levels on top and bottom, which should not be there (see Figure 1 below, matplotlib-output). Now, when I export the arrays I used in Python to Matlab (i.e. the exactly same data set that was used to generate the matplotlib-contourf-plot) and use Matlab's contourf-function, I get a slightly different output, without those sharp contour-level edges (see Figure 2 below, Matlab-output). I used the same number of levels in both figures. In figure 3 I have made a scatterplot of the same data, which shows that there are no such sharp edges in the data as shown in the contourf-plot (I added contour-lines just for reference). Example dataset can be downloaded through Dropbox-link given below. The data set contains three txt-files: X, Y, Z. Each of them are an 500x500 arrays, which can be directly used with contourf(), i.e. plt.contourf(X,Y,Z,...). The code that used was

plt.contourf(X,Y,Z,10, cmap=plt.cm.jet)
plt.contour(X,Y,Z,10,colors='black', linewidths=0.5)
plt.axis('equal')
plt.axis('off')

Does anyone have an idea why this happens? I would appreciate any insight on this!

Cheers,

Jussi

Below are the details of my setup:


Python 3.7.0

IPython 6.5.0

matplotlib 2.2.3


Matplotlib output Matplotlib output

Matlab output Matlab output

Matplotlib-scatter Link to data set

Jmgeon
  • 11
  • 3
  • I suppose there is no datapoint on the edge where the difference is observed. Without a datapoint there the contour algorithm has the freedom to let the lines cross whereever it wants. For a defined behaviour you will need to make sure there is some data available for the position in question. Possibly further details may be given if a [mcve] was provided. – ImportanceOfBeingErnest Oct 23 '18 at 16:22
  • Hi and thanks for the input. I however do not think that it is a case of data points - the original contourf-image was ran with a meshgrid of 500 x 500 data points and I repeated the computations with 1000 x 1000 and 10000 x 10000 data points, with the same result. Thus the reason cannot be the number of data points...I also made a test by just making a scatterplot of the data and colouring the points with the values and this did not results in the same sharp edge as seen in the contourf-plot – Jmgeon Oct 24 '18 at 10:05
  • Interesting. I suppose you may want to provide a mock-up dataset which shows the issue for further investigation then. – ImportanceOfBeingErnest Oct 24 '18 at 13:44
  • I added an link to the data set - link can be found below the main text. @ImportanceOfBeingErnest – Jmgeon Oct 25 '18 at 13:55

1 Answers1

0

The confusing thing about the matlab plot is that its colorbar shows much more levels than there are actually in the plot. Hence you don't see the actual intervals that are contoured.

You would achieve the same result in matplotlib by choosing 12 instead of 11 levels.

import numpy as np
import matplotlib.pyplot as plt

X, Y, Z = [np.loadtxt("data/roundcontourdata/{}.txt".format(i)) for i in list("XYZ")]

levels = np.linspace(Z.min(), Z.max(), 12)
cntr  = plt.contourf(X,Y,Z,levels, cmap=plt.cm.jet)
plt.contour(X,Y,Z,levels,colors='black', linewidths=0.5)
plt.colorbar(cntr)
plt.axis('equal')
plt.axis('off')

plt.show()

enter image description here

So in conclusion, both plots are correct and show the same data. Just the levels being automatically chosen are different. This can be circumvented by choosing custom levels depending on the desired visual appearance.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thanks, this clarified the reason for the observed behaviour. Much appreciated! @ImportanceOfBeingErnest – Jmgeon Nov 05 '18 at 11:59