首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子图中的分类数据

子图中的分类数据
EN

Stack Overflow用户
提问于 2015-08-30 16:17:31
回答 1查看 1.2K关注 0票数 0

我有一个pandas数据帧,其中包含20列,其中混合了数字和分类数据。我想绘制一个5x4矩阵的数据图。使用matplotlib和子图,我现在有了所有数字数据的图,但对于我的生活,我不知道如何包括分类数据。

我想要像这样的东西

代码语言:javascript
复制
df['RBC'].value_counts().plot(kind='bar')

而是在次要情节中。

以下是一些代码(为简洁起见,我省略了一些重复部分)。

代码语言:javascript
复制
from rdkit.Chem import AllChem as Chem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem import PandasTools
from rdkit.Chem import Draw
import pybel
import pandas as pd
import matplotlib
import matplotlib.pyplot as p
import matplotlib.ticker as plticker
%matplotlib inline 


#commandline application to calculate properties
output = !/Applications/ChemAxon/MarvinBeans/bin/evaluate /Users/username/Desktop/SampleFiles/Fragments.sdf -g -e "field('IDNUMBER'); molString('smiles'); logp(); logd('7.4'); apka('1'); bpka('1'); atomCount(); mass(); acceptorcount(); donorcount(); topologicalPolarSurfaceArea(); rotatablebondcount(); refractivity(); ASAHydrophobic('7.4'); ASAPolar('7.4'); atomCount()-atomCount('1');aromaticAtomCount()/(atomCount()-atomCount('1'))"

[line.split(';') for line in output]

cols = ['ID', 'smiles', 'logP', 'logD', 'apKa', 'bpKa', 'atomCount', 'mass', 'HBA', 'HBD', 'TPSA', 'RBC', 'MR', 'ASAh', 'ASAp', 'HAC', 'FractionAromatic']
df = pd.DataFrame([line.split(';') for line in output], columns=cols)
df = df.convert_objects(convert_numeric=True)

#series of calculations using the calculated data to add several categorical numeric and text fields to dataframe.

myLogP = df['logP']
myLogD = df['logD']
myMass = df['mass']
myTPSA = df['TPSA']
myRBC = df['RBC']
myRBCmax = max(myRBC) +1
myHBA = df['HBA']
myHBAmax = max(myHBA) +1
myHBD = df['HBD']
myHBDmax = max(myHBD) +1
myHAC = df['HAC']
myHACmax= range(min(myHAC), max(myHAC) + 1)


myFraromatic = df['FractionAromatic']

fig, axes = plt.subplots(nrows=5, ncols=4)
ax0, ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8, ax9, ax10, ax11, ax12, ax13, ax14, ax15, ax17, ax18, ax19 = axes.flat
axis_font = {'fontname':'Arial', 'size':'14'}
title_font = {'fontname':'Arial', 'size':'14', 'color' :'blue'}

loc = plticker.MultipleLocator(base=1.0)

ax0.hist(myLogP, histtype='bar')
ax0.set_title('LogP', title_font)
ax0.set_xlabel('Range of LogP', axis_font)
ax0.set_ylabel('Count')

ax1.hist(myLogD, histtype='bar')
ax1.set_title('LogD', title_font)
ax1.set_xlabel('Range of LogD', axis_font)
ax1.set_ylabel('Count', axis_font)

ax2.hist(myMass, histtype='bar', color = 'red')
ax2.set_title('Mass', title_font)
ax2.set_xlabel('Range of MWt', axis_font)
ax2.set_ylabel('Count', axis_font)

ax3.hist(myTPSA,  histtype='bar', color = 'yellow')
ax3.set_title('TPSA', title_font)
ax3.set_xlabel('Range of TPSA', axis_font)
ax3.set_ylabel('Count', axis_font)

#etc.

#ax8 'AZBN' is a categorical text field 

ax9.hist(myFraromatic, bins= 10, histtype='bar')
ax9.set_title('Aromatic', title_font)
ax9.set_xlabel('Fraction of Aromatic atoms', axis_font)
ax9.set_ylabel('Count', axis_font)

#further categorical plots




fig.set_size_inches(20, 15)

plt.tight_layout()

plt.show()
EN

回答 1

Stack Overflow用户

发布于 2015-08-30 19:14:36

你真的应该发布你已经尝试过的代码和一些样本数据。否则,不可能知道最好的方法。但是,我认为您可能希望尝试以下方法,该方法使用matplotlib API而不是pandas,并使您能够更好地控制每个绘图中的内容:

代码语言:javascript
复制
from matplotlib import pyplot as plt
fig, axes = plt.subplots(5, 4)   # axes is a numpy array of pyplot Axes
axes = iter(axes.ravel())   # set up an iterator for the set of axes. 

categoricals = df.columns[df.dtypes == 'category']
numeric = df.columns[df.dtypes != 'category']

for col in categoricals: 
    ax = df[col].value_counts().plot(kind='bar', ax=axes.next())
    # do other stuff with ax, formatting etc.  
    # the plot method returns the axis used for the plot for further manipulation

for col in numeric: 
     ax = df[col].plot(ax=axes.next())
     # etc. 

这只是给你一些想法,因为我不知道你的数据的细节,你想如何绘制每一列,你有什么类型的数据等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32294586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档