2

I would need some help with my Matplotlib contour plots.

The problem is, that the lines of my contour plot (shown beneath) are not closing up and that I have an image kind of cut in half. I am wondering if I can force Matplotlib to close the contours or if my data has too poor statistics to do so. However, I selected all objects with a value of the x-axis > 10.0 and got 10e6 objects to plot.

Figure shows contour lines and filled contours which are cut at some point and not closing up.

I am using:

  • Python 2.7
  • Ubuntu 14.04
  • --> plot printed with PdfPages

The code is the following:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.backends.backend_pdf import PdfPages

x        = np.linspace(min(mydata[:,0]), max(mydata[:,0]), 100)
y        = np.linspace(min(mydata[:,1]), max(mydata[:,1]), 100)

H        = np.histogram2d(mydata[:,0], mydata[:,1], bins=100)       
binsize  = int((H.max()-grid_min)/8)

X,Y      = np.meshgrid(x,y, indexing='ij')

#set_zero=2.0 % for example! Amount of objects which are not included in the plot 
#in order to get a not that wide spread last contour (dark blue in the figure enclosed)!    
grid_min = np.floor(int(H.max()/100.0*set_zero))        

CS=cb_axis.contour(X,Y,H, 8, lw=2.5, ls='-', col='w', 
   levels=np.arange(grid_min, np.ceil(H.max()), binsize))

cb_axis.contourf(X,Y,H, 8, levels=np.arange(grid_min, np.ceil(H.max()), 
   binsize), cmap='coolwarm'))       

plt.colorbar(CS, pad=0.01, aspect=35, use_gridspec=True, format='%0.1e')
firefly2517
  • 115
  • 2
  • 15

1 Answers1

1

In principle there can be two reasons for the contour lines not to be closed.

  1. The first is a simple visual appearance. Having white lines on a white background may make the look like disappear. This effect is shown in the plot below in the first image, left side of the data. This effect can be visualized by coloring the contour lines (second image).

There is not much you can do about this, appart from changing the lines' or the background's color.

  1. The second reason, which can be observed in the first two images on the right side of the data, is that the lines are really not closed, because they are at the edge of the data. This is of course independent of the lines' color and is due to matplotlib not knowing what to do outside the data (it's only the human brain that thinks those lines should be closed, but there is no evidence for closed lines in the data).

To solve this second problem, one can extend the data or assign the edge values of the data to a minimum value, such that the lines need to close. This is shown in the third and fourth example image, where I set the last column of the data matrix to zero.

enter image description here

For reference, this is the code to produce the above image.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.backends.backend_pdf import PdfPages

n=90
x        = np.arange(n)
y        = np.arange(n)
X,Y      = np.meshgrid(x,y)
H        = (np.sinc((X-70)/50.)**2*np.sinc((Y-50)/50.)**2)*3600
H[:, :50] = np.zeros((n,50))

grid_min = 10
binsize = H.max()/8.        

fig, (ax, ax2, ax3, ax4) = plt.subplots(ncols=4, figsize=(12,5))
plt.subplots_adjust(left=0.03, right=0.97, wspace=0.03)
levels=np.arange(grid_min, np.ceil(H.max()), binsize)
for a in (ax, ax2, ax3, ax4):
    a.contourf(X,Y,H, levels=levels, cmap='viridis')
    a.set_xlim([40,100])
    a.yaxis.set_visible(False)
    a.xaxis.set_visible(False)

ax.set_title("Open white lines")
ax.text(.076, .5, "Closed white line ", rotation=90, transform=ax.transAxes, va="center")
ax.text(.86, .5, "Open white lines", rotation=-90, transform=ax.transAxes, va="center")     
ax.contour(X,Y,H, levels=levels, linewidths=3, linestyles='-', colors=['w' for i in range(len(levels))]) 

ax2.set_title("Open Colored lines")
ax2.text(.06, .5, "Closed colored lines", rotation=90, transform=ax2.transAxes, va="center")
ax2.text(.86, .5, "Open colored lines", rotation=-90, transform=ax2.transAxes, va="center")
ax2.contour(X,Y,H, levels=levels, linewidths=3, linestyles='-', cmap="jet") 

# Setting the last column of data to zero in order to close the lines
H[:, n-1] = np.zeros(n)
ax3.set_title("Closed White lines")
ax3.text(.076, .5, "Closed white lines", rotation=90, transform=ax3.transAxes, va="center")
ax3.text(.86, .5, "Closed white lines", rotation=-90, transform=ax3.transAxes, va="center")    
ax3.contour(X,Y,H, levels=levels, linewidths=3, linestyles='-', colors=['w' for i in range(len(levels))]) 

ax4.set_title("Closed Colored lines")
ax4.text(.06, .5, "Closed colored lines", rotation=90, transform=ax4.transAxes, va="center")
ax4.text(.86, .5, "Closed colored lines", rotation=-90, transform=ax4.transAxes, va="center")   
ax4.contour(X,Y,H, levels=levels, linewidths=3, linestyles='-', cmap="jet") 

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712