python 绘图颜色_带有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], ['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运行(实际上我有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).为此,我在函数中手动更改了颜色,但是我希望有一种自动执行此操作的解决方案.而且我知道我的方法确实很糟糕而且很丑陋,我只是不知道该怎么做.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值