使用 seaborn 为数据框绘制直方图
Plotting histogram using seaborn for a dataframe
我有一个包含多列和多行的数据框。许多行没有列值,因此在数据框中它表示为 NaN。
示例dataFrame如下,
df.head()
GEN Sample_1 Sample_2 Sample_3 Sample_4 Sample_5 Sample_6 Sample_7 Sample_8 Sample_9 Sample_10 Sample_11 Sample_12 Sample_13 Sample_14
A123 9.4697 3.19689 4.8946 8.54594 13.2568 4.93848 3.16809 NAN NAN NAN NAN NAN NAN NAN
A124 6.02592 4.0663 3.9218 2.66058 4.38232 NAN NAN NAN NAN NAN NAN NAN
A125 7.88999 2.51576 4.97483 5.8901 21.1346 5.06414 15.3094 2.68169 8.12449 NAN NAN NAN NAN NAN
A126 5.99825 10.2186 15.2986 7.53729 4.34196 8.75048 16.9358 5.52708 NAN NAN NAN NAN NAN NAN
A127 28.5014 4.86702 NAN NAN NAN NAN NAN NAN NAN NAN NAN NAN NAN NAN
我想使用来自 python 的 seaborn 函数绘制此数据帧的直方图,所以我尝试了以下几行,
sns.set(color_codes=True)
sns.set(style="white", palette="muted")
sns.distplot(df)
但是它抛出以下错误,
ValueError Traceback (most recent call last)
<ipython-input-80-896d7fe85ef3> in <module>()
1 sns.set(color_codes=True)
2 sns.set(style="white", palette="muted")
----> 3 sns.distplot(df)
/anaconda3/lib/python3.4/site-packages/seaborn/distributions.py in distplot(a, bins, hist, kde, rug, fit, hist_kws, kde_kws, rug_kws, fit_kws, color, vertical, norm_hist, axlabel, label, ax)
210 hist_color = hist_kws.pop("color", color)
211 ax.hist(a, bins, orientation=orientation,
--> 212 color=hist_color, **hist_kws)
213 if hist_color != color:
214 hist_kws["color"] = hist_color
/anaconda3/lib/python3.4/site-packages/matplotlib/axes/_axes.py in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
5627 color = mcolors.colorConverter.to_rgba_array(color)
5628 if len(color) != nx:
-> 5629 raise ValueError("color kwarg must have one color per dataset")
5630
5631 # We need to do to 'weights' what was done to 'x'
ValueError: color kwarg must have one color per dataset
任何 helps/suggestions 消除此错误将不胜感激..!!!
假设我有您上面显示的数据的摘录(唯一不同的是在我的机器上 NAN
是 NaN
)。
然后,我能想到的最好的图形表示是分组条形图:每个样本一组,每组内有基因条(有些人偶尔称之为直方图)
为此,您首先需要在 R
客厅 "melt" 您的数据,即 "long"。然后,您就可以继续绘图了。
data = df.set_index('GEN').unstack().reset_index()
data.columns = ['sample','GEN', 'value']
sns.set(style="white")
g = sns.factorplot(x='sample'
,y= 'value'
,hue='GEN'
,data=data
,kind='bar'
,aspect=2
)
g.set_xticklabels(rotation=30);
请告诉我们这是否是您想要的情节类型。
我还以为seaborn
documentation提到可以同时绘制多列,默认情况下用颜色突出显示。
但是重读之后,我什么也没看到。相反,我想我是从 this tutorial 中推断出来的,在教程的一部分中,教程绘制了一个包含多列的数据框。
但是,"solution" 是微不足道的,希望正是您要查找的内容:
sns.set(color_codes=True)
sns.set(style="white", palette="muted")
sns.distplot(df)
for col_id in df.columns:
sns.distplot(df[col_id])
默认情况下,这将改变颜色,"knowing"已经使用过的颜色。
注意:我使用了不同的数据集,因为我不确定如何重新创建你的数据集。
我遇到了类似的问题,因为我的 pandas.DataFrame 在我想绘制的列中有 Object 类型的元素(my_column)。这样命令:
print(df[my_column])
给我:
Length: 150, dtype: object
解决方案是
sns.distplot(df[my_column].astype(float))
作为 my_column 的数据类型转换为:
Length: 150, dtype: float64
我有一个包含多列和多行的数据框。许多行没有列值,因此在数据框中它表示为 NaN。 示例dataFrame如下,
df.head()
GEN Sample_1 Sample_2 Sample_3 Sample_4 Sample_5 Sample_6 Sample_7 Sample_8 Sample_9 Sample_10 Sample_11 Sample_12 Sample_13 Sample_14
A123 9.4697 3.19689 4.8946 8.54594 13.2568 4.93848 3.16809 NAN NAN NAN NAN NAN NAN NAN
A124 6.02592 4.0663 3.9218 2.66058 4.38232 NAN NAN NAN NAN NAN NAN NAN
A125 7.88999 2.51576 4.97483 5.8901 21.1346 5.06414 15.3094 2.68169 8.12449 NAN NAN NAN NAN NAN
A126 5.99825 10.2186 15.2986 7.53729 4.34196 8.75048 16.9358 5.52708 NAN NAN NAN NAN NAN NAN
A127 28.5014 4.86702 NAN NAN NAN NAN NAN NAN NAN NAN NAN NAN NAN NAN
我想使用来自 python 的 seaborn 函数绘制此数据帧的直方图,所以我尝试了以下几行,
sns.set(color_codes=True)
sns.set(style="white", palette="muted")
sns.distplot(df)
但是它抛出以下错误,
ValueError Traceback (most recent call last)
<ipython-input-80-896d7fe85ef3> in <module>()
1 sns.set(color_codes=True)
2 sns.set(style="white", palette="muted")
----> 3 sns.distplot(df)
/anaconda3/lib/python3.4/site-packages/seaborn/distributions.py in distplot(a, bins, hist, kde, rug, fit, hist_kws, kde_kws, rug_kws, fit_kws, color, vertical, norm_hist, axlabel, label, ax)
210 hist_color = hist_kws.pop("color", color)
211 ax.hist(a, bins, orientation=orientation,
--> 212 color=hist_color, **hist_kws)
213 if hist_color != color:
214 hist_kws["color"] = hist_color
/anaconda3/lib/python3.4/site-packages/matplotlib/axes/_axes.py in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
5627 color = mcolors.colorConverter.to_rgba_array(color)
5628 if len(color) != nx:
-> 5629 raise ValueError("color kwarg must have one color per dataset")
5630
5631 # We need to do to 'weights' what was done to 'x'
ValueError: color kwarg must have one color per dataset
任何 helps/suggestions 消除此错误将不胜感激..!!!
假设我有您上面显示的数据的摘录(唯一不同的是在我的机器上 NAN
是 NaN
)。
然后,我能想到的最好的图形表示是分组条形图:每个样本一组,每组内有基因条(有些人偶尔称之为直方图)
为此,您首先需要在 R
客厅 "melt" 您的数据,即 "long"。然后,您就可以继续绘图了。
data = df.set_index('GEN').unstack().reset_index()
data.columns = ['sample','GEN', 'value']
sns.set(style="white")
g = sns.factorplot(x='sample'
,y= 'value'
,hue='GEN'
,data=data
,kind='bar'
,aspect=2
)
g.set_xticklabels(rotation=30);
请告诉我们这是否是您想要的情节类型。
我还以为seaborn
documentation提到可以同时绘制多列,默认情况下用颜色突出显示。
但是重读之后,我什么也没看到。相反,我想我是从 this tutorial 中推断出来的,在教程的一部分中,教程绘制了一个包含多列的数据框。
但是,"solution" 是微不足道的,希望正是您要查找的内容:
sns.set(color_codes=True)
sns.set(style="white", palette="muted")
sns.distplot(df)
for col_id in df.columns:
sns.distplot(df[col_id])
默认情况下,这将改变颜色,"knowing"已经使用过的颜色。
注意:我使用了不同的数据集,因为我不确定如何重新创建你的数据集。
我遇到了类似的问题,因为我的 pandas.DataFrame 在我想绘制的列中有 Object 类型的元素(my_column)。这样命令:
print(df[my_column])
给我:
Length: 150, dtype: object
解决方案是
sns.distplot(df[my_column].astype(float))
作为 my_column 的数据类型转换为:
Length: 150, dtype: float64