title: Density plot of chaos game using Python matplotlib?
body: <p>Essentially all I'm trying to do is produce as set of points via an IFS and use a color map to show the multiplicity of each point. In other words, if we assume a color map where high values are more yellow and lower ones are more red, then values repeatedly produced by the IFS will be more yellow. </p>
  
  <p>I'm struggling to get correct results for this. Each thing I've tried has resulted in an image that looks interesting, but is clearly incorrect as it differs wildly from what you get from simply plotting the points without color mapping.  </p>
  
  <p>Below is the base code that I'm comfortable with, without the failed attempts at color mapping. What can I do to get a proper color map? </p>
  
  <p>The basic strategy, I think, is to make a matrix 'mat' holding the point multiplicities and do something like plt.imshow(xs, ys, c=mat. cmap="..."). I've tried different approaches to this but keep coming up with incorrect results.</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  import random
  
  def f(x, y, n):
      N = np.array([[x, y]])
  
      M = np.array([[1, 0], [0, 1]])
  
      b = np.array([[.5], [0]])
      b2 = np.array([[0], [.5]])
  
      if n == 0:
          return np.dot(M, N.T)
  
      elif n == 1:
         return np.dot(M, N.T) + b
  
      elif n == 2:
          return np.dot(M, N.T) + b2
  
      elif n == 3:
          return np.dot(M, N.T) - b
  
      elif n == 4:
          return np.dot(M, N.T) - b2
  
  
  xs = [] # x coordinates
  ys = [] # y coordinates
  D = {}  # point multiplicities 
  
  random.seed()
  
  x = 1
  y = 1
  
  for i in range(0, 100000):
      n = random.randint(1, 4)
  
      V = f(x, y, n)
  
      x = V.item(0)
      y = V.item(1)
  
      xs.append(x)
      ys.append(y)
  
      xi = round(x, 3)
      yi = round(y, 3)
  
      if (xi, yi) in D:
          D[(xi, yi)] += 1
      else:   
          D[(xi, yi)] = 1
  
  plt.xlabel('x')
  plt.ylabel('y')
  
  plt.scatter(xs,ys, s=.05)
  plt.autoscale(True, True, True)
  plt.show()
  </code></pre>