Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用matplotlib+;熊猫_Python_Pandas_Matplotlib_Ggplot2_Seaborn - Fatal编程技术网

Python 使用matplotlib+;熊猫

Python 使用matplotlib+;熊猫,python,pandas,matplotlib,ggplot2,seaborn,Python,Pandas,Matplotlib,Ggplot2,Seaborn,我正在分析一个两因子(M)方差分析;抽样设计包括两个分类变量,分别为两个和三个水平,回答维度为4。在python中完成了所有的数据解析之后,我想继续在python中绘制数据。(而不是切换到R进行绘图)虽然我的代码不仅非常冗长,而且整个过程看起来和感觉都像是一个非常糟糕的黑客。我的问题:熊猫matplotlib制作以下情节的方式是什么?出于兴趣:我也很高兴看到不使用seaborn的解决方案 R中的解决方案(绘图为2行代码): 这将生成以下绘图: 我当前的python解决方案: # Numeri

我正在分析一个两因子(M)方差分析;抽样设计包括两个分类变量,分别为两个和三个水平,回答维度为4。在python中完成了所有的数据解析之后,我想继续在python中绘制数据。(而不是切换到R进行绘图)虽然我的代码不仅非常冗长,而且整个过程看起来和感觉都像是一个非常糟糕的黑客。我的问题:熊猫matplotlib制作以下情节的方式是什么?出于兴趣:我也很高兴看到不使用seaborn的解决方案

R中的解决方案(绘图为2行代码):

这将生成以下绘图:

我当前的python解决方案:

 # Numerics
 import numpy as np
 from numpy.random import randint

 # Data managment
 import pandas as pd
 from pandas import DataFrame
 from pandas import Series

 # Plotting
 import matplotlib
 matplotlib.use('Qt4Agg')
 import matplotlib.pyplot as pt
 import seaborn as sns

 # Creating sample data
 np.random.seed(12345)
 index = pd.Index(np.arange(42))
 frame = DataFrame(np.random.randn(42,4) + np.array([10,3,5,1]), columns=['Base', 'State23', 'State42', 'End'], index=index)
 genotype = Series(['WT', 'HET'], name='genotype', dtype='category')
 environment = Series(['heavySmoker', 'casualSmoker', 'nonSmoker'], name='environment', dtype='category')
 gen = genotype[np.random.randint(2, size=42)]
 env = environment[np.random.randint(3, size=42)]
 gen.index = frame.index
 env.index = frame.index
 frame['genotype'] = gen
 frame['environment'] = env

 # Plotting the data
 response = ['Base', 'State23', 'State42', 'End']
 fig, ax = pt.subplots(1, len(response), sharex=True, sharey=True)
 for i, r in enumerate(response):
     sns.boxplot(data=frame, x='genotype', y=r, hue='environment', ax=ax[i])
     ax[i].set_ylabel('')
     ax[i].set_title(r)

 fig.subplots_adjust(wspace=0)
 fig.savefig('boxplot-python.png')
这将生成以下绘图:


正如您可能同意的那样,代码不仅冗长,而且也没有真正做到我想要的。例如,我不知道如何删除图例的多重外观,x轴上的标签也很奇怪。

编辑后使用
factorplot
而不是mwaskom在评论中建议的
FaceGrid

如果使用数据帧,则可以利用Seaborn的:

您可以在melt函数中根据需要重命名“值”和“变量”

以下是结果图表:


上一个答案带有
FaceGrid

g = sns.FacetGrid(df, col="variable", size=4, aspect=.7)
g.map(sns.boxplot, "genotype", "value", "environment").add_legend(title="environment")

更易于使用
factorplot
。比我的解决方案好多了。。这个传说出现在一个奇怪的地方,不过,在最后一个面中间,与方框图重叠,这样你就什么也看不到,而且在小脚本里也一样。我试过:p=factorplot(…,legend_out=True);p、 fig.show()但不幸的是,我想在情节之外画图例的愿望被忽略了。你也是这样吗?你知道怎么把传说放在别的地方吗?我已经把我的照片放进去了。这个传说看起来不错,但不确定是什么原因造成了这种差异。我使用的是IPython 3.4、pandas 0.17.1和seaborn 0.7.0。您可以尝试使用
size
aspect
。尺寸越大,字体越小(因为大图片被压缩)。方面可能有助于为传奇腾出空间。。。我使用了
size=4,aspect=.7
,但即使使用默认值,图例也在外部。我使用的是:ipython==4.2.0 ipython genutils==0.1.0 matplotlib==1.5.1 pandas==0.18.1 seaborn==0.7.0
df = pd.melt(frame, id_vars=['genotype', 'environment'])

sns.factorplot(data=df, x='genotype', y='value', 
               hue='environment', col='variable', 
               kind='box', legend=True)
g = sns.FacetGrid(df, col="variable", size=4, aspect=.7)
g.map(sns.boxplot, "genotype", "value", "environment").add_legend(title="environment")