Python 使用seaborn绘制QQ图的FaceGrid

Python 使用seaborn绘制QQ图的FaceGrid,python,matplotlib,plot,statistics,seaborn,Python,Matplotlib,Plot,Statistics,Seaborn,我画不出一张画 我有一个由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")

我画不出一张画

我有一个由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
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
39 plt.close()
40
--->41 qqplot(“lognorm_qqplot”,np.log2(expr),“对数正常qqplot”)
在QQplot中(路径、表达式、标题)
34
35 g=sns.FaceGrid(expr\u m,col=“Feature”,col\u wrap=n\u cols)
--->36 g.map(分位数图,“值”);
37 plt.savefig(fpath+“.pdf”,bbox_inches=“tight”)
38 plt.savefig(fpath+“.png”,bbox_inches=“紧密”)
/地图中的usr/local/lib/python3.5/site-packages/seaborn/axisgrid.py(self、func、*args、**kwargs)
726
727#画出情节
-->728自切面图(func、ax、图参数、kwargs)
729
730#完成注释和布局
/usr/local/lib/python3.5/site-packages/seaborn/axisgrid.py in_facet_plot(self、func、ax、plot_args、plot_kwargs)
810
811#画出情节
-->812 func(*绘图参数,**绘图参数)
813
814#整理支持信息
分位数图中(y,**kwargs)
25 y=ss.zscore(y)
26 qntls,xr=ss.probplot(y,dist=“norm”)
--->27 plt.散射(xr,qntls,**kwargs)
28
29 expr\u m=局部熔化(expr)
/usr/local/lib/python3.5/site-packages/matplotlib/pyplot.py散点(x、y、s、c、标记、cmap、norm、vmin、vmax、alpha、线宽、顶点、边色、保持、数据、**kwargs)
3249 vmin=vmin,vmax=vmax,alpha=alpha,
3250线宽=线宽,顶点=顶点,
->3251 edgecolors=edgecolors,data=data,**kwargs)
3252最后:
3253斧头保持(洗旧)
/usr/local/lib/python3.5/site packages/matplotlib/_______.py位于内部(ax、*args、**kwargs)
1810警告。警告(消息%(标签名称,功能名称),
1811运行时警告,堆栈级别=2)
->1812返回函数(ax,*args,**kwargs)
1813预付款单=内部付款单__
1814如果pre_doc为无:
/usr/local/lib/python3.5/site-packages/matplotlib/axes//u axes.py散点(self、x、y、s、c、marker、cmap、norm、vmin、vmax、alpha、线宽、顶点、边色、**kwargs)
3838 y=np.ma.ravel(y)
3839如果x.size!=y、 尺寸:
->3840提升值错误(“x和y必须大小相同”)
3841
3842 s=np.ma.ravel(s)#这不必匹配x,y的大小。
ValueError:x和y的大小必须相同

我做到了这一点,还将颜色更改为使用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")

“ss”是全局变量还是模块?!哎呀,忘记添加了。它是
scipy.stats
。编辑thanks@fbrundu不是答案,但您可能想看看我是如何在这里实现的:当从
statsmodels.api
应用
qqqplot
时,这个答案似乎不正确。我得到一个空白的绘图网格,后面是每个单独的
qqplot
。我现在无法测试它。如果代码不再有效,请发布另一个答案。谢谢