2016-07-07 7 views
1

QQ-plotsseabornで描画することはできません。シーボーン付きQQプロットのFacetGridを描画

私はm行(観測値)とn列(フィーチャ)の行列を持ち、各フィーチャ(列)のQQプロットを描き、正規分布と比較したいと思います。

はこれまでのところ、私のコードは次のようである:

import scipy.stats as ss 

def qqplots(fpath, expr, title): 

    def quantile_plot(x, **kwargs): 
     x = ss.zscore(x) 
     qntls, xr = ss.probplot(x, dist="norm") 
     plt.scatter(xr, qntls, **kwargs) 

    expr_m = pd.melt(expr) 
    expr_m.columns = ["Feature", "Value"] 
    n_feat = len(expr_m["Feature"].value_counts().index) 

    n_cols = int(np.sqrt(n_feat)) + 1 

    g = sns.FacetGrid(expr_m, col="Feature", col_wrap=n_cols) 
    g.map(quantile_plot, "Value"); 
    plt.savefig(fpath + ".pdf", bbox_inches="tight") 
    plt.savefig(fpath + ".png", bbox_inches="tight") 
    plt.close() 

qqplots("lognorm_qqplot", np.log2(expr), "Log-normal qqplot") 

expr変数はm行(観測)n列(機能)とパンダのデータフレームです。

私が手例外は以下の通りです:

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-52-f9333a55702e> in <module>() 
    39  plt.close() 
    40 
---> 41 qqplots("lognorm_qqplot", np.log2(expr), "Log-normal qqplot") 

<ipython-input-52-f9333a55702e> in qqplots(fpath, expr, title) 
    34 
    35  g = sns.FacetGrid(expr_m, col="Feature", col_wrap=n_cols) 
---> 36  g.map(quantile_plot, "Value"); 
    37  plt.savefig(fpath + ".pdf", bbox_inches="tight") 
    38  plt.savefig(fpath + ".png", bbox_inches="tight") 

/usr/local/lib/python3.5/site-packages/seaborn/axisgrid.py in map(self, func, *args, **kwargs) 
    726 
    727    # Draw the plot 
--> 728    self._facet_plot(func, ax, plot_args, kwargs) 
    729 
    730   # Finalize the annotations and layout 

/usr/local/lib/python3.5/site-packages/seaborn/axisgrid.py in _facet_plot(self, func, ax, plot_args, plot_kwargs) 
    810 
    811   # Draw the plot 
--> 812   func(*plot_args, **plot_kwargs) 
    813 
    814   # Sort out the supporting information 

<ipython-input-52-f9333a55702e> in quantile_plot(y, **kwargs) 
    25   y = ss.zscore(y) 
    26   qntls, xr = ss.probplot(y, dist="norm") 
---> 27   plt.scatter(xr, qntls, **kwargs) 
    28 
    29  expr_m = pd.melt(expr) 

/usr/local/lib/python3.5/site-packages/matplotlib/pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, hold, data, **kwargs) 
    3249       vmin=vmin, vmax=vmax, alpha=alpha, 
    3250       linewidths=linewidths, verts=verts, 
-> 3251       edgecolors=edgecolors, data=data, **kwargs) 
    3252  finally: 
    3253   ax.hold(washold) 

/usr/local/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs) 
    1810      warnings.warn(msg % (label_namer, func.__name__), 
    1811         RuntimeWarning, stacklevel=2) 
-> 1812    return func(ax, *args, **kwargs) 
    1813   pre_doc = inner.__doc__ 
    1814   if pre_doc is None: 

/usr/local/lib/python3.5/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs) 
    3838   y = np.ma.ravel(y) 
    3839   if x.size != y.size: 
-> 3840    raise ValueError("x and y must be the same size") 
    3841 
    3842   s = np.ma.ravel(s) # This doesn't have to match x, y in size. 

ValueError: x and y must be the same size 
+0

は「SS」ですグローバル変数かモジュールか? – giosans

+0

追加するのを忘れてしまいました。それは 'scipy.stats'です。編集ありがとう – gc5

+1

@fbrundu答えはありませんが、私がこれをどのように実装したかを見てみるといいかもしれません:http://phobson.github.io/mpl-probscale/tutorial/closer_look_at_viz.html#mapping-probability-plots-to-海軍 - ファセットギルド –

答えて

1

私はこれを達成し、また次のコードで、Seabornカラーパレットを使用する色を変更:

def qqplots(fpath, expr, title): 

    def quantile_plot(x, **kwargs): 
     x = ss.zscore(x) 
     ss.probplot(x, plot=plt) 

    expr_m = pd.melt(expr) 
    expr_m.columns = ["Feature", "Value"] 
    n_feat = len(expr_m["Feature"].value_counts().index) 

    n_cols = int(np.sqrt(n_feat)) + 1 

    g = sns.FacetGrid(expr_m, col="Feature", col_wrap=n_cols) 
    g.map(quantile_plot, "Value"); 
    for ax in g.axes: 
     ax.get_lines()[0].set_markerfacecolor(sns.color_palette()[0]) 
     ax.get_lines()[1].set_color(sns.color_palette()[3]) 
    plt.savefig(fpath + ".pdf", bbox_inches="tight") 
    plt.savefig(fpath + ".png", bbox_inches="tight") 
    plt.close() 

qqplots("lognorm_qqplot", np.log2(expr), "Log-normal qqplot") 
関連する問題