2017-09-06 12 views
0

私は助けが必要です、私はグループ化されたバープロットを作ろうとしました。 しかし、それは動作しません。 最初はすべてのタブフォルダを読み込み、x軸に2列( '特別な名前'と 'ab')の列が必要です。 'special name'には3種類の名前しかありませんが、すべてに4または7の 'ab'があります。グループ化されたバープロットPython

Example tab file 
Names names_id first second special_name ab 
lili  1   a  b Tm   a 
Katrin 2   c  d Tm   u 
Paul  3   e  f ui   f 
bob  4   g  h ui   b 
tina  5   i  j ac   a 

そして、y軸は、それぞれの「ab」がどれくらいの頻度でカウントされるかを教えてください。

%matplotlib inline 
import matplotlib as mpl 
from matplotlib.gridspec import GridSpec 
import matplotlib.pyplot as plt 
import sys 
import os 
import glob 
import seaborn as sns 
import pandas as pd 
import ggplot 
from ggplot import aes 

sns.set(style= "whitegrid", palette="pastel", color_codes=True) 

tab_folder = 'myData' 
out_folder ='myData/plots' 
tab = glob.glob('%s/R*.tab'%(tab_folder)) 

#is reading all my data 
for i, tab_file in enumerate(tab): 
    folder,file_name=os.path.split(tab_file) 
    s_id=file_name[:-4].replace('DD','') 
    df=pd.DataFrame.from_csv(tab_file, sep='\t') 
    df_2 = df.groupby(['special_name','ab']).size().reset_index(name='count') 

    #Here I wanted to create grouped barplots 
    ggplot(df_2, aes(x=('special_name'), y=('count'), fill=('ab'))) + geom_bar(stat='identity',position='dodge') 

    ax.set_title(s_id) 
    ax.set_xlabel('') 
    ax.set_ylabel('') 

png_t = '%s/%s.b.png'%(out_folder,s_id) 
plt.savefig(png_t, dpi = 500) 

マイコードでもエラーは発生しません。しかし、私は空のグリッドだけを取得....何が間違っているのですか?

私はggplot.ggplotを試してみてください():

AttributeError:         Traceback (most recent call last) 
<ipython-input-33-03dc98f5428a> in <module>() 
    100 
    101  #barplots 
--> 102  ggplot.ggplot(df_2, aes(x=('special_name'), y=('count'), fill=('ab'))) + geom_bar(stat='identity',position='dodge') 

はAttributeError:型オブジェクトのggplot 'には属性 'ggplot' を持っていない

+1

'ggplot.ggplot()'のようなものが必要だと思いますが、モジュール自体を呼び出そうとしています。 –

+0

私はggplot.ggplot()を試しましたが、動作しませんでした。私はあなたが意味することを知っていると思うが、私は他の変種を知らない。 – Fox

+0

エラーについて報告するときは、問題の[mcve]と完全なエラートレースバック(最後の行だけでなく)が必要です。上記のMartinEvansのコメントは絶対的に正しいものであり、現在のところ唯一のヒントである。 – ImportanceOfBeingErnest

答えて

0

私は私のミスを発見! ggplotは必要ありません。私は自分のdf_2を修正する必要がありました

%matplotlib inline 
import matplotlib as mpl 
from matplotlib.gridspec import GridSpec 
import matplotlib.pyplot as plt 
import sys 
import os 
import glob 
import seaborn as sns 
import pandas as pd 
import ggplot 
from ggplot import aes 

sns.set(style= "whitegrid", palette="pastel", color_codes=True) 

tab_folder = 'myData' 
out_folder ='myData/plots' 
tab = glob.glob('%s/R*.tab'%(tab_folder)) 

#is reading all my data 
for i, tab_file in enumerate(tab): 
    folder,file_name=os.path.split(tab_file) 
    s_id=file_name[:-4].replace('DD','') 
    df=pd.DataFrame.from_csv(tab_file, sep='\t') 


    #Here I create grouped barplots 
    df_2 = df.groupby(['special_name','ab']).size().reset_index(name='count') 
    table = pd.pivot_table(df_2, index='special_name', columns='ab', values='count') 
    table.plot(kind='barh', color= ['r', 'g', 'b', 'k', 'm', 'y'], ax = ax) 

    ax.set_title(s_id) 
    ax.set_xlabel('') 
    ax.set_ylabel('') 

png_t = '%s/%s.b.png'%(out_folder,s_id) 
plt.savefig(png_t, dpi = 500) 
関連する問題