大家一天中的好时光!我正在编写一个简单的脚本,用于质量分析,比较原始和复制的样本,并将其绘制在散点图上。
到目前为止,我已经能够创建我需要的图:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
'''read file'''
duplicates_file = 'C:/Users/cherp2/Desktop/duplicates.csv'
duplicates = pd.read_csv(
duplicates_file, usecols=['SAMPLE_NUMBER','Duplicate Sample Type'
,'FE', 'P','SIO2','AL2O3'
,'Orig. Sample Type', 'FE.1', 'P.1'
,'SIO2.1','AL2O3.1'])
'''calculate standard deviations for grades'''
grades = ['FE','P','SIO2','AL2O3']
for grade in grades:
grade_std = duplicates[grade].std()
'''create scatter plots for all grades'''
ax = duplicates.plot.scatter(f'{grade}', f'{grade}.1')
ax.set_xlabel('Original sample')
ax.set_ylabel('Duplicate sample')但现在我想根据一个条件给图点着色:如果原始样本和复制样本之间的等级差异小于一个标准差值,则绿色应为绿色,如果介于2和3 stdev之间,则应为橙色,如果大于该值,则为红色。
我一直试图在网上寻找解决方案,但到目前为止还没有奏效。我有一种感觉,我需要在这里使用一些lambda函数,但我不确定语法。
发布于 2019-09-09 11:38:20
您可以将颜色参数传递给绘图调用(通过c=),并使用pandas.cut为基于标准的不同类别生成必要的颜色代码。
In [227]: df
Out[227]:
a b
0 0.991415 -0.627043
1 1.365594 -0.036651
2 -0.376318 -0.536504
3 1.041561 -2.180642
4 1.017692 -0.308826
5 -0.626566 1.613980
6 -1.302070 1.258944
7 -0.453499 0.411277
8 -0.927880 0.439102
9 -0.282031 1.249862
10 0.504829 0.536641
11 -1.528550 1.420456
12 0.774111 -1.086350
13 -1.662715 0.732753
14 -1.038514 -1.987912
15 -0.432515 3.104590
16 1.682876 0.663448
17 0.287642 -1.038507
18 -0.307923 -2.340498
19 -1.024045 -1.948608
In [228]: change = df.a - df.b
In [229]: df.plot(kind='scatter', x='a', y='b',
c=pd.cut(((change - change.mean()) / (change.std())).abs(), [0, 1, 2, 3], labels=['r', 'g', 'b']))

https://stackoverflow.com/questions/57847345
复制相似问题