Python 2.7 散点图图例-循环中的颜色条?

Python 2.7 散点图图例-循环中的颜色条?,python-2.7,matplotlib,scatter-plot,Python 2.7,Matplotlib,Scatter Plot,目前我有如下代码设置: import matplotlib.pyplot as plt import numpy as np plt.close('all') nSub = 17 nMonths = 12 nYears=16 datalist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q'] DATA = np.random.rand(nSub,nMonths

目前我有如下代码设置:

import matplotlib.pyplot as plt
import numpy as np

plt.close('all')

nSub = 17
nMonths = 12
nYears=16

datalist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q']

DATA   = np.random.rand(nSub,nMonths,nYears)
DATA1  = np.random.rand(nSub,nMonths,nYears)

DIFF = DATA  - DATA1

fig, ax = plt.subplots (nrows=1, ncols=1)

for imonth in range(12):
    x = imonth + 1
    x_sub = np.linspace(x-0.5, x+0.5, nSub) 

    for isub in range(len(datalist)):
        x_plot = np.ones((nYears,), dtype=np.float32)*x_sub[isub]
        y_plot = DIFF[isub, imonth, :]
        ax.scatter(x_plot, y_plot, label=datalist[isub]) 
        ax.set_xticks(np.arange(1.0, 12.+1, 1.0))
plt.legend()
plt.show()
其图形如下所示:

x轴表示月份(N个月),散点图表示16年内每月重复17个不同数据集(nSub)

我遇到的问题是,我不知道如何正确地分配散点图颜色,这些颜色只会给数据列表(isub)中的每个数据集每个月使用相同的颜色(对于所有的nyear,每个imonth都相同)

这意味着,每个月每个“a”都是相同的颜色。最后,我应该只有17种(nSub)颜色和相同数量的元素在我的图例中


如何解决这个问题?

是否使用了
seaborn
选项?不,很遗憾,我在运行代码的地方无法访问它:/n不确定seaborn是否有任何帮助(至少在他们发布新的scattery功能之前是这样)。但我在理解你想在这张图中显示什么方面也有问题。这主要是因为您似乎在用于打印的循环中操纵数据。最好先完整地生成数据,然后尝试绘制该数据。