[英]Create python pandas histograms for specific row range as well as iterating through columns?
我有一组要转置的数据,以将数据组划分为行,并在列中具有占位符值(就我而言,是一系列质量值)。 我的下一个目标是为包含相同字符的每一行绘制直方图,如下所示:
mz 902.4 909.4 915.3
n 0.6 0.3 1.4
n.1 0.4 0.3 1.3
n.2 0.3 0.2 1.1
n.3 0.2 0.2 1.3
n.4 0.4 0.3 1.4
DCIS 0.3 1.6
DCIS.1 0.3 1.2
DCIS.2 1.1
DCIS.3 0.2 1.2
DCIS.4 0.2 1.3
DCIS.5 0.2 0.1 1.5
br_1 0.5 0.4 1.4
br_1.1 0.2 1.3
br_1.2 0.5 0.2 1.4
br_1.3 0.5 0.2 1.6
br_1.4 1.4
我的目标是为字母n为第1组,DCIS为第2组等的人绘制从902.4开始的直方图,这些组应在同一直方图中。 然后,我计划遍历各列重复相同的过程,因此代码应产生相同数量的直方图列。
下面是到目前为止的代码(输入文件是转置前的excel xlsx文件):
nh = pd.ExcelFile(nheight)
df = pd.read_excel(nh, index=False)
dfn = df.filter(like='n', axis=0)
dfbr1234 = df.filter(like='br', axis=0)
plt.figure()
plt.hist([dfn, dfbr1234], bins=50)
plt.show()
我试图将带有字母“ br”的行分组在一起以进行测试,但是它会产生零大小的数组,以减少最小化操作,并且没有身份错误。
编辑:所以数据框是上面的表。
我想做的是绘制一个直方图,其中包含3个单独的直方图,在上面的屏幕截图中分别用黑色,红色和橙色框指定。 目的是比较单个图中的不同方框,我想进行迭代,以便对其他两列(图片中的第2列和第3列)执行相同的操作。 我尝试使用df.filter函数过滤'like ='n''等等,但是我不确定如何结合使用不同的过滤数据以及遍历各列。 上面的代码还没有迭代,但是我正在考虑利用iloc [:,variable]进行迭代。
这是一种基本方法
df = pd.read_clipboard()
df = df.fillna(0)
print(df)
mz 902.4 909.4 915.3
0 n 0.6 0.3 1.4
1 n.1 0.4 0.3 1.3
2 n.2 0.3 0.2 1.1
3 n.3 0.2 0.2 1.3
4 n.4 0.4 0.3 1.4
5 DCIS 0.3 1.6 0.0
6 DCIS.1 0.3 1.2 0.0
7 DCIS.2 1.1 0.0 0.0
8 DCIS.3 0.2 1.2 0.0
9 DCIS.4 0.2 1.3 0.0
10 DCIS.5 0.2 0.1 1.5
11 br_1 0.5 0.4 1.4
12 br_1.1 0.2 1.3 0.0
13 br_1.2 0.5 0.2 1.4
14 br_1.3 0.5 0.2 1.6
15 br_1.4 1.4 0.0 0.0
制作子集(如果可以很好地定义逻辑,则可以进入下面的迭代),
df_n = df.loc[df['mz'].str.startswith('n')]
df_D = df.loc[df['mz'].str.startswith('D')]
df_b = df.loc[df['mz'].str.startswith('b')]
使用matplotlib
的subplots()
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=df.shape[1]-1,ncols=1)
plt.tight_layout()
for i in range(1,df.shape[1]):
df_n.iloc[:,i].hist(ax=ax[i-1],color = 'k', alpha=0.4) # reduced alpha because you're plotting many histograms on top of each other
df_D.iloc[:,i].hist(ax=ax[i-1],color = 'r', alpha=0.4)
df_b.iloc[:,i].hist(ax=ax[i-1],color = 'orange', alpha=0.4)
ax[i-1].set_title("Histograms for " + df.columns[i])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.