Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is an illustrative example of <a href="http://matplotlib.org/examples/pylab_examples/custom_cmap.html" rel="noreferrer">how to create custom colormaps here</a>. The docstring is essential for understanding the meaning of <code>cdict</code>. Once you get that under your belt, you might use a <code>cdict</code> like this:</p> <pre><code>cdict = {'red': ((0.0, 1.0, 1.0), (0.1, 1.0, 1.0), # red (0.4, 1.0, 1.0), # violet (1.0, 0.0, 0.0)), # blue 'green': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'blue': ((0.0, 0.0, 0.0), (0.1, 0.0, 0.0), # red (0.4, 1.0, 1.0), # violet (1.0, 1.0, 0.0)) # blue } </code></pre> <p>Although the <code>cdict</code> format gives you a lot of flexibility, I find for simple gradients its format is rather unintuitive. Here is a utility function to help generate simple LinearSegmentedColormaps:</p> <pre><code>import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as mcolors def make_colormap(seq): """Return a LinearSegmentedColormap seq: a sequence of floats and RGB-tuples. The floats should be increasing and in the interval (0,1). """ seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3] cdict = {'red': [], 'green': [], 'blue': []} for i, item in enumerate(seq): if isinstance(item, float): r1, g1, b1 = seq[i - 1] r2, g2, b2 = seq[i + 1] cdict['red'].append([item, r1, r2]) cdict['green'].append([item, g1, g2]) cdict['blue'].append([item, b1, b2]) return mcolors.LinearSegmentedColormap('CustomMap', cdict) c = mcolors.ColorConverter().to_rgb rvb = make_colormap( [c('red'), c('violet'), 0.33, c('violet'), c('blue'), 0.66, c('blue')]) N = 1000 array_dg = np.random.uniform(0, 10, size=(N, 2)) colors = np.random.uniform(-2, 2, size=(N,)) plt.scatter(array_dg[:, 0], array_dg[:, 1], c=colors, cmap=rvb) plt.colorbar() plt.show() </code></pre> <p><img src="https://i.stack.imgur.com/3ULpI.png" alt="enter image description here"></p> <hr> <p>By the way, the <code>for-loop</code></p> <pre><code>for i in range(0, len(array_dg)): plt.plot(array_dg[i], markers.next(),alpha=alpha[i], c=colors.next()) </code></pre> <p>plots one point for every call to <code>plt.plot</code>. This will work for a small number of points, but will become extremely slow for many points. <code>plt.plot</code> can only draw in one color, but <code>plt.scatter</code> can assign a different color to each dot. Thus, <code>plt.scatter</code> is the way to go.</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