-3

I am solving an optimization problem. There are n iterations and at each one I have a point x_n=[x_n1, x_n2]. I want to plot the iterates x_n so that as n increases the color of the points gets dark or lighter or whatever. Currently I can plot the iterates but they are all the same color so i cannot tell which points correspond to higher values of n.

The variable x_test is an array which contains all the iterates from x_0 to x_n. Here is my code to plot it:

pl.scatter(x_test[:,0], x_test[:,1])
pl.show()

I have found a lot of info on how to color graphs but not in the way I am desiring, where each point corresponds to a different light/darkness.

Here is a minimal, complete, verifiable example:

import numpy as np
import pylab as pl
x = np.array([[1,1],[1,2],[1,3],[2,4]])
pl.plot(x[:,0], x[:,1])

This gives the scatter plot of the points in the array x but I need them to each be a different color corresponding to the position in x, i.e. [1,1] should be the lightest, then [1,2] will be slightly dark, etc until [2,4] is the darkest.

edit: here is the solution I went with:

scaled_itera = np.array(range(x_test.shape[0]))/(float(x_test.shape[0])-1)
colors = pl.cm.coolwarm(scaled_itera)

pl.scatter(x_test[:,0], x_test[:,1], color=colors)
pl.show()

2 Answers2

1

You can directly give a list of color to plt.scatter(). For example: you can do:

import seaborn as sns
color_list = sns.color_palette("Paired", n_colors=x_test.shape[0])
plt.scatter(x_test[:,0], x_test[:,1], color=color_list)

Check colormap_reference if you want to stay with matplotlib for colors.

An example using plt.cm:

plt.scatter(x_test[:,0], x_test[:,1], color=plt.cm.Paired.colors[:x_test.shape[0]])
Zhiya
  • 610
  • 2
  • 7
  • 22
  • 1
    I think `color` also takes scalar values and applies a palette internally. No need for seaborn then :) – MB-F Apr 05 '18 at 13:05
  • I don't have this module seaborn, I would prefer to stay within the confines of numpy/pylab – mordecai iwazuki Apr 05 '18 at 13:07
  • You do not need to use `seaborn` or `palettable`. You can directly use a list of colors but I just like them because they provide predefined palettes. See the link in my updated answer there. – Zhiya Apr 05 '18 at 13:08
  • Okay it's not clear to me how to do it without seaborn/palettable. I can deduce that I will use, for example, cmap = 'autumn' but what should be the entry for color = ? – mordecai iwazuki Apr 05 '18 at 13:17
  • Check the link I posted in the answer above to use the color maps in `matplotlib`. – Zhiya Apr 05 '18 at 13:21
  • Yeah it's not helpful to me, they plot all of them at once and it's hard for me to see what's going on. I figured it out using the comment on the question anyways, I will post my solution. – mordecai iwazuki Apr 05 '18 at 13:24
  • It's straightforward. See my edited answer. – Zhiya Apr 05 '18 at 13:31
-1

you need to loop through each point

assuming your data are something like

x = [[1,2],[3,4],[5,6],[7,8]]

then, something like

c_aray = ['black','green','red','blue']
for i in range(len(x)):
    xn = x[i]
    plt.plot(xn[0],xn[1],color = c_aray[i],'o')
Mohammad Athar
  • 1,953
  • 1
  • 15
  • 31
  • This I can do but I have thousands of iterates so i will need a way for the color list, c_aray, to be generated automatically – mordecai iwazuki Apr 05 '18 at 13:05
  • thousands might be hard, but dozens is possible; start with a hexadecimal code, and increment it "smartly". this will get you off in the right direction: https://www.thedataschool.co.uk/gwilym-lockwood/viridis-colours-tableau/ – Mohammad Athar Apr 05 '18 at 13:13
  • In fact there is an easier way than to loop over all the points, I edited the solution into the question. – mordecai iwazuki Apr 05 '18 at 13:30