gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:18 24 4
gpt4 key购买 nike

我有以下数据框

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

如您所见,我的数据分布在不同的盘子上。我想创建尽可能多的情节,因为我有不同的板 block :3 个情节。在每个图上,我想将一个盘子涂成红色,将其他盘子涂成黑色。

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

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)

enter image description here所以在这里我有我的第一个图,红色的板“RAC1”和黑色的 RAC3 和 RAC8,现在我想要第二个图,RAC3 为红色(RAC1 和 RAC8 为黑色),第三个图为 RAC8红色(黑色的 RAC1 和 RAC3)。为此,我手动更改函数中的颜色,但我想要一个自动执行的解决方案。而且我知道我的方式确实是一种糟糕而丑陋的方式,我只是不知道该怎么做。

最佳答案

您可以使用 groupby这里结合difference pandas Index 对象循环遍历您的盘子并获取当前盘子和其余盘子的索引:

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()

enter image description here enter image description here enter image description here

关于python - 使用 matplotlib 的绘图上的颜色条件数据引发了循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43048218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com