Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://matplotlib.sourceforge.net/users/image_tutorial.html" rel="noreferrer">Matplotlib</a> has the <code>imshow</code> method for plotting arrays:</p> <pre><code>from matplotlib import mpl,pyplot import numpy as np # make values from -5 to 5, for this example zvals = np.random.rand(100,100)*10-5 # make a color map of fixed colors cmap = mpl.colors.ListedColormap(['blue','black','red']) bounds=[-6,-2,2,6] norm = mpl.colors.BoundaryNorm(bounds, cmap.N) # tell imshow about color map so that only set colors are used img = pyplot.imshow(zvals,interpolation='nearest', cmap = cmap,norm=norm) # make a color bar pyplot.colorbar(img,cmap=cmap, norm=norm,boundaries=bounds,ticks=[-5,0,5]) pyplot.show() </code></pre> <p>This is what it looks like: </p> <p><img src="https://i.stack.imgur.com/qnV0Z.png" alt="enter image description here"></p> <p>The details for the color bar setup were taken from a matplotlib example: <a href="http://matplotlib.sourceforge.net/examples/api/colorbar_only.html" rel="noreferrer">colorbar_only.py.</a> It explains that the number of <code>boundaries</code> need to be one larger then then number of colors. </p> <p><strong>EDIT</strong> </p> <p>You should <a href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.imshow" rel="noreferrer">note</a>, that <code>imshow</code> accepts the <code>origin</code> keyword, which sets the where the first point is assigned. The default is 'upper left', which is why in my posted plot the y axis has 0 in the upper left and 99 (not shown) in the lower left. The alternative is to set <code>origin="lower"</code>, so that first point is plotted in the lower left corner. </p> <p><strong>EDIT 2</strong></p> <p>If you want a gradient and not a discrete color map, make a color map by <a href="http://matplotlib.sourceforge.net/api/colors_api.html#matplotlib.colors.LinearSegmentedColormap.from_list" rel="noreferrer">linearly interpolating</a> through a series of colors:</p> <pre><code>fig = pyplot.figure(2) cmap2 = mpl.colors.LinearSegmentedColormap.from_list('my_colormap', ['blue','black','red'], 256) img2 = pyplot.imshow(zvals,interpolation='nearest', cmap = cmap2, origin='lower') pyplot.colorbar(img2,cmap=cmap2) fig.savefig("image2.png") </code></pre> <p>This produces: <img src="https://i.stack.imgur.com/bCttt.png" alt="enter image description here"></p> <p><strong>EDIT 3</strong></p> <p>To add a grid, as shown in this <a href="http://matplotlib.sourceforge.net/examples/pylab_examples/axes_props.html" rel="noreferrer">example</a>, use the <code>grid</code> method. Setting the grid color to 'white' works well with the colors used by the colormap (ie the default black does not show up well). </p> <pre><code>pyplot.grid(True,color='white') </code></pre> <p>Including this before the <code>savefig</code> call produces this plot (made using 11x11 grid for clarity): <img src="https://i.stack.imgur.com/oPpby.png" alt="enter image description here"> There are many options for <code>grid</code>, which are described in the matplotlib <a href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.grid" rel="noreferrer">documentation</a>. One you might be interested in is <code>linewidth</code>.</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.
 

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