0

I have developed the following MWE with data that spreads into two very different scales. I would like to make a contourplot which allows nicely visualising the data.

from matplotlib import ticker
import numpy as np               
import matplotlib.pyplot as plt 
data = np.random.rand(25,25)
data2 = np.random.rand(25,25)*1e-32
data = np.hstack([data,data2])
xGrid, yGrid = np.meshgrid(np.linspace(0,1,data.shape[1]),np.linspace(0,1,data.shape[0]))

levels=np.logspace(np.log10(1e-6),np.log10(2),100)
locator = ticker.LogLocator(base=10)
cs = plt.contourf(xGrid, yGrid, data, levels, vmin = 1e-6, vmax = 2, locator=locator)
plt.colorbar(cs, ticks=locator)

With the above code, I get : enter image description here

I don't get why half the values are blank

FenryrMKIII
  • 1,068
  • 1
  • 13
  • 30

1 Answers1

3

Ok I found out what is the issue thanks to another post. One needs to use the option extend = "both". Unfortunately, this option does not work with logarithm scales.

The solution is to manually rescale the data range. An example is provided below :

from matplotlib import ticker
import numpy as np               
import matplotlib.pyplot as plt 
data = np.random.rand(25,25)
data2 = np.random.rand(25,25)*1e-32
data = np.hstack([data,data2])
xGrid, yGrid = np.meshgrid(np.linspace(0,1,data.shape[1]),np.linspace(0,1,data.shape[0]))

levels=np.logspace(np.log10(1e-16),np.log10(2),100)
locator = ticker.LogLocator(base=10)

#mask data
dataMasked = np.where(data < 1e-16, 1e-16, data)

cs = plt.contourf(xGrid, yGrid, dataMasked, levels, vmin = 1e-16, vmax = 2, extend="both", locator=locator)
plt.colorbar(cs, ticks=locator)
FenryrMKIII
  • 1,068
  • 1
  • 13
  • 30