1

Here is the data for clarity sakes:

x_values = [22000, 33000, 36000, 49000, 53000, 59000, 60000, 63000, 64000, 64000, 65000, 65000, 66000, 66000, 69000, 69000, 69000, 70000, 70000, 72000, 73000, 73000, 78000, 82000, 82000, 86000, 86000, 86000, 87000, 87000, 87000, 89000, 90000, 91000, 91000, 95000, 95000, 96000, 96000, 97000, 99000, 100000, 100000, 101000, 101000, 103000, 105000, 105000, 105000, 106000, 110000, 112000, 113000, 113000, 114000, 114000, 116000, 116000, 116000, 117000, 117000, 117000, 118000, 119000, 119000, 119000, 119000, 120000, 120000, 120000, 121000, 121000, 121000, 121000, 121000, 123000, 128000, 129000, 130000, 130000, 132000, 132000, 136000, 142000, 143000, 144000, 145000, 154000, 154000, 154000, 154000, 157000, 161000, 161000, 161000, 164000, 164000, 164000, 172000, 173000, 179000, 180000, 180000]

y_values =  [29900, 26500, 17250, 10500, 20495, 19500, 19500, 19800, 15995, 15995, 10480, 10480, 18410, 20500, 16500, 11500, 11999, 11500, 11999, 6600, 21995, 21995, 17500, 17991, 11500, 19500, 11995, 11995, 14300, 14300, 15600, 15000, 7495, 16895, 16895, 11500, 16590, 13000, 16590, 13000, 16903, 15500, 16550, 16595, 16595, 15495, 10688, 14980, 17990, 12988, 12988, 15500, 13991, 15500, 14895, 16588, 14990, 14900, 14995, 5500, 13995, 16790, 16790, 13500, 13000, 13995, 15900, 11000, 13500, 13000, 11000, 16495, 16550, 16895, 17950, 13995, 13888, 13980, 8990, 8990, 10995, 11995, 6999, 14000, 12000, 12000, 10995, 3000, 3700, 3900, 14000, 11995, 11500, 12990, 13790, 6700, 6700, 14995, 13900, 13900, 8000, 7500, 8000]

year_values = [2006, 2006, 2005, 2005, 2006, 2005, 2005, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2005, 2006, 2006, 2006, 2006, 2005, 2006, 2006, 2006, 2005, 2006, 2005, 2006, 2006, 2005, 2005, 2006, 2005, 2006, 2005, 2005, 2006, 2006, 2005, 2006, 2005, 2006, 2005, 2006, 2006, 2006, 2006, 2005, 2006, 2006, 2005, 2005, 2006, 2005, 2006, 2005, 2006, 2005, 2006, 2006, 2006, 2006, 2006, 2006, 2005, 2006, 2006, 2006, 2005, 2005, 2006, 2005, 2006, 2006, 2006, 2006, 2005, 2006, 2006, 2005, 2005, 2005, 2005, 2005, 2006, 2006, 2006, 2005, 2005, 2005, 2005, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2005, 2005, 2005, 2005, 2005]

I am trying to graph X against Y, and use the year_values as a label. As in, each year is a different color, all plotted on the same graph.

The only way I can think of doing this, as it stands right now, is by converting year_values into a set and then using if statements to plot for each different year_value. That doesn't seem to be the most elegant solution.

Any idea how to best achieve this?

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
DGDD
  • 1,370
  • 7
  • 19
  • 36
  • I'm looking for something more along these lines: http://stackoverflow.com/questions/16006572/plotting-different-colors-in-matplotlib – DGDD Feb 22 '14 at 18:24

1 Answers1

3

One approach is to do something similar to this:

fig, ax= plt.subplots()
for year in np.unique(years):
    mask = years == year
    ax.plot(x[mask], y[mask], linestyle='none', marker='o', label=year)
ax.legend(numpoints=1)

You could also use pandas to make the grouping a touch simpler, but if you're not already using pandas, it's overkill for this purpose.

As a complete example (using your data):

import numpy as np
import matplotlib.pyplot as plt

x = np.array([22000, 33000, 36000, 49000, 53000, 59000, 60000, 63000, 64000,
64000, 65000, 65000, 66000, 66000, 69000, 69000, 69000, 70000, 70000, 72000,
73000, 73000, 78000, 82000, 82000, 86000, 86000, 86000, 87000, 87000, 87000,
89000, 90000, 91000, 91000, 95000, 95000, 96000, 96000, 97000, 99000, 100000,
100000, 101000, 101000, 103000, 105000, 105000, 105000, 106000, 110000, 112000,
113000, 113000, 114000, 114000, 116000, 116000, 116000, 117000, 117000, 117000,
118000, 119000, 119000, 119000, 119000, 120000, 120000, 120000, 121000, 121000,
121000, 121000, 121000, 123000, 128000, 129000, 130000, 130000, 132000, 132000,
136000, 142000, 143000, 144000, 145000, 154000, 154000, 154000, 154000, 157000,
161000, 161000, 161000, 164000, 164000, 164000, 172000, 173000, 179000, 180000,
180000])

y = np.array([29900, 26500, 17250, 10500, 20495, 19500, 19500, 19800, 15995,
15995, 10480, 10480, 18410, 20500, 16500, 11500, 11999, 11500, 11999, 6600,
21995, 21995, 17500, 17991, 11500, 19500, 11995, 11995, 14300, 14300, 15600,
15000, 7495, 16895, 16895, 11500, 16590, 13000, 16590, 13000, 16903, 15500,
16550, 16595, 16595, 15495, 10688, 14980, 17990, 12988, 12988, 15500, 13991,
15500, 14895, 16588, 14990, 14900, 14995, 5500, 13995, 16790, 16790, 13500,
13000, 13995, 15900, 11000, 13500, 13000, 11000, 16495, 16550, 16895, 17950,
13995, 13888, 13980, 8990, 8990, 10995, 11995, 6999, 14000, 12000, 12000,
10995, 3000, 3700, 3900, 14000, 11995, 11500, 12990, 13790, 6700, 6700, 14995,
13900, 13900, 8000, 7500, 8000])

years = np.array([2006, 2006, 2005, 2005, 2006, 2005, 2005, 2006, 2006, 2006,
2006, 2006, 2006, 2006, 2005, 2006, 2006, 2006, 2006, 2005, 2006, 2006, 2006,
2005, 2006, 2005, 2006, 2006, 2005, 2005, 2006, 2005, 2006, 2005, 2005, 2006,
2006, 2005, 2006, 2005, 2006, 2005, 2006, 2006, 2006, 2006, 2005, 2006, 2006,
2005, 2005, 2006, 2005, 2006, 2005, 2006, 2005, 2006, 2006, 2006, 2006, 2006,
2006, 2005, 2006, 2006, 2006, 2005, 2005, 2006, 2005, 2006, 2006, 2006, 2006,
2005, 2006, 2006, 2005, 2005, 2005, 2005, 2005, 2006, 2006, 2006, 2005, 2005,
2005, 2005, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2005, 2005, 2005,
2005, 2005])

fig, ax= plt.subplots()
for year in np.unique(years):
    mask = years == year
    ax.plot(x[mask], y[mask], linestyle='none', marker='o', label=year)
ax.legend(numpoints=1)
plt.show()

enter image description here

Joe Kington
  • 275,208
  • 71
  • 604
  • 463