Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use a <code>ListedColormap</code> to specify the white and red as the only colors in the color map, and the bounds determine where the transition is from one color to the next:</p> <pre><code>import matplotlib.pyplot as plt from matplotlib import colors import numpy as np np.random.seed(101) zvals = np.random.rand(100, 100) * 10 # make a color map of fixed colors cmap = colors.ListedColormap(['white', 'red']) bounds=[0,5,10] norm = colors.BoundaryNorm(bounds, cmap.N) # tell imshow about color map so that only set colors are used img = plt.imshow(zvals, interpolation='nearest', origin='lower', cmap=cmap, norm=norm) # make a color bar plt.colorbar(img, cmap=cmap, norm=norm, boundaries=bounds, ticks=[0, 5, 10]) plt.savefig('redwhite.png') plt.show() </code></pre> <p>The resulting figure has only two colors:</p> <p><img src="https://i.stack.imgur.com/SPJ9N.png" alt="enter image description here"></p> <p>I proposed essentially the same thing for a somewhat different question: <a href="https://stackoverflow.com/q/7229971/717357">2D grid data visualization in Python</a></p> <p>The solution is inspired by a <a href="http://matplotlib.sourceforge.net/examples/api/colorbar_only.html" rel="noreferrer">matplotlib example</a>. The example explains that the <code>bounds</code> must be one more than the number of colors used. </p> <p>The <a href="http://matplotlib.sourceforge.net/api/colors_api.html#matplotlib.colors.BoundaryNorm" rel="noreferrer"><code>BoundaryNorm</code></a> is a normalization that maps a series of values to integers, which are then used to assign the corresponding colors. <code>cmap.N</code>, in the example above, just defines the number of colors. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload