Python MatPlotLib:给定一个数据集3,绘制2并使用第3个作为标签

Python MatPlotLib:给定一个数据集3,绘制2并使用第3个作为标签,python,matplotlib,plot,Python,Matplotlib,Plot,为了清楚起见,以下是数据: 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,

为了清楚起见,以下是数据:

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]
我试图将X与Y进行对比,并使用year_值作为标签。与中一样,每年都是不同的颜色,都绘制在同一个图表上

我能想到的唯一方法是,将year_值转换成一个集合,然后使用if语句为每个不同的year_值绘图。这似乎不是最优雅的解决方案


你知道如何最好地实现这一点吗?

一种方法是做类似的事情:

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)
您也可以使用
pandas
来简化分组,但是如果您还没有使用
pandas
,这就太过分了

作为完整示例(使用您的数据):


我正在寻找更多与此相关的东西:
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()