Python 使用matplotlib的绘图上的颜色条件数据引发循环

Python 使用matplotlib的绘图上的颜色条件数据引发循环,python,pandas,matplotlib,Python,Pandas,Matplotlib,我在下面的数据帧中有一个 import pandas as pd import matplotlib.pyplot as plt datas = [['RAC1','CD0287',1.52,9.88], ['RAC1','CD0695',2.08,10.05],['RAC1','CD0845',2.01,10.2], ['RAC3','CD0258',1.91,9.8], ['RAC3','CD471',1.66,9.6], ['RAC8','CD0558',1.32,9.3], ['RAC

我在下面的数据帧中有一个

import pandas as pd
import matplotlib.pyplot as plt

datas = [['RAC1','CD0287',1.52,9.88], ['RAC1','CD0695',2.08,10.05],['RAC1','CD0845',2.01,10.2], ['RAC3','CD0258',1.91,9.8], ['RAC3','CD471',1.66,9.6], ['RAC8','CD0558',1.32,9.3], ['RAC8','CD0968',2.89,10.01]]
labels = ['Plate', 'Sample', 'LogRatio', 'Strength']
df = pd.DataFrame(data = datas, columns=labels, index=[8, 3, 5, 4, 12, 44, 2])
print(df)

    Plate  Sample  LogRatio  Strength
8   RAC1  CD0287      1.52      9.88
3   RAC1  CD0695      2.08     10.05
5   RAC1  CD0845      2.01     10.20
4   RAC3  CD0258      1.91      9.80
12  RAC3   CD471      1.66      9.60
44  RAC8  CD0558      1.32      9.30
2   RAC8  CD0968      2.89     10.01
如你所见,我的数据分布在不同的板块上。 我想创造尽可能多的阴谋,因为我有不同的板块:3个阴谋。在每个情节上,我想把一个盘子涂成红色,其他的涂成黑色

到目前为止,我找到的唯一方法是手动编写每个图版的代码,并将红色图版更改为earch run(实际上我有30多个图版,所以需要花费太多时间)。如果我的代码可以帮助您理解以下内容,我仍然可以向您展示我的代码:

def getIndexPlates(df):
    listIndicesAllPlates = []
    df = df.reset_index()
    for name,group in df.groupby("Plate"): 
        temp_list = []
        temp_list.append(name)
        temp_list.append(group.index.tolist()) #create a tuple with the name of the plate and the index of all the samples in this plate
        listIndexAllPlates.append(temp_list)
    return listIndexAllPlates

def plotting(df,listIndexAllPlates): 
    plt.clf()
    ax=plt.gca()
    datas = df[["LogRatio", "Strength"]].as_matrix()
    for sample in range(len(datas)):
        if sample in listIndexAllPlates[0][1]: #if the sample is on the the first tuple of my list -> on the first plate
            ax.scatter(datas[sample,0], datas[sample,1], alpha=0.8, facecolors='none', edgecolors='red')
        if sample in listIndexAllPlates[1][1]:
            ax.scatter(datas[sample,0], datas[sample,1], alpha=0.8, facecolors='none', edgecolors='black')
        if sample in listIndexAllPlates[2][1]:
            ax.scatter(datas[sample,0], datas[sample,1], alpha=0.8, facecolors='none', edgecolors='black')
    plt.show()

listIndexAllPlates = getIndexPlates(df)
plotting(df,listIndexAllPlates)
因此,这里我有我的第一个图版“RAC1”为红色,RAC3和RAC8为黑色,现在我想有第二个图版,RAC3为红色(RAC1和RAC8为黑色),第三个图版,RAC8为红色(RAC1和RAC3为黑色)。为此,我在我的函数中手动更改颜色,但我希望有一个自动更改颜色的解决方案。我知道我的方式真的很糟糕很丑陋,我只是不知道怎么做

您可以在此处与熊猫索引对象一起使用,以循环浏览您的板块,并获取当前板块和其余板块的索引:

for label, plate_df in df.groupby("Plate"):
    plate_indices = plate_df.index
    rest_indices = df.index.difference(plate_indices)

    # do your plotting here accordingly

    print(label, plate_indices, rest_indices)

RAC1 Int64Index([8, 3, 5], dtype='int64') Int64Index([2, 4, 12, 44], dtype='int64')
RAC3 Int64Index([4, 12], dtype='int64') Int64Index([2, 3, 5, 8, 44], dtype='int64')
RAC8 Int64Index([44, 2], dtype='int64') Int64Index([3, 4, 5, 8, 12], dtype='int64')
编辑 要包含打印,只需包含matplotlib语句:

plot_kwargs = {"alpha": 0.8, "facecolors": "none"}
for label, plate_df in df.groupby("Plate"):
    plate_indices = plate_df.index
    rest_indices = df.index.difference(plate_indices)

    # create plot
    plt.clf()
    ax=plt.gca()
    ax.scatter(df.loc[plate_indices, "LogRatio"], df.loc[plate_indices, "Strength"], edgecolors='red', **plot_kwargs)
    ax.scatter(df.loc[rest_indices, "LogRatio"], df.loc[rest_indices, "Strength"], edgecolors='black', **plot_kwargs)
    plt.show()

您可以使用
df.plate.values
循环浏览plate列的值,并在iloc函数的帮助下,获得每个板的行。然后你就用你的绘图函数。我可能误解了你的意图。无论如何,使用类似于df.Plate.values中p的
命令:
打印df.iloc[df['Plate']==p]
您可以获得每个板的行,我认为您可以从中获得所需的内容:由于您的回答,我可以避免使用
getIndexPlates
函数直接获取每个图版的行,但自动更改每个循环处的红色图版以创建多个绘图并没有帮助。