pandas 如何将数据从 np 矩阵加载到 seaborn?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29201153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load data from np matrix to seaborn?
提问by Denis
I want to build own Scatterplot Matrixwith labelsand colors. I like this example: Scatterplot Matrix.
我想Scatterplot Matrix用labels和建立自己的colors。我喜欢这个例子:散点图矩阵。
I have one problem. I don't understand, how exactly to load data from numpy matrixto seaborn dataframe.
我有一个问题。我不明白,究竟如何从numpy matrixto加载数据seaborn dataframe。
data_resc = np.random.rand(150,2)
sns.set()
df = DataFrame(data_resc)
sns.pairplot(df, hue="species", size=2.5)
sns.plt.show()
This code snippet has errors:
此代码片段有错误:
Traceback (most recent call last):
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3687)
File "pandas\hashtable.pyx", line 381, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7192)
TypeError: an integer is required
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "file.py", line 69, in <module>
main()
File "file.py", line 64, in main
sns.pairplot(df, hue="species", size=2.5)
File "C:\Python34\lib\site-packages\seaborn\linearmodels.py", line 1720, in pairplot
size=size, aspect=aspect, dropna=dropna, **grid_kws)
File "C:\Python34\lib\site-packages\seaborn\axisgrid.py", line 857, in __init__
hue_names = np.unique(np.sort(data[hue]))
File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 1780, in __getitem__
return self._getitem_column(key)
File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 1787, in _getitem_column
return self._get_item_cache(key)
File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 1068, in _get_item_cache
values = self._data.get(item)
File "C:\Python34\lib\site-packages\pandas\core\internals.py", line 2849, in get
loc = self.items.get_loc(item)
File "C:\Python34\lib\site-packages\pandas\core\index.py", line 1402, in get_loc
return self._engine.get_loc(_values_from_object(key))
File "pandas\index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas\index.c:3807)
File "pandas\index.pyx", line 156, in pandas.index.IndexEngine.get_loc (pandas\index.c:3744)
KeyError: 'species'
回答by JohnE
I think you loaded your data from numpy to a pandas dataframe correctly, if you just leave off hue, it should work:
我认为您将数据从 numpy 正确加载到 Pandas 数据框,如果您只是离开hue,它应该可以工作:
sns.pairplot(df, size=2.5)


I've had problems with huethough. I thought this would work, and it kind of does, but there is a lot of error/warning code and the existing plot is 3x3 instead of 2x2. In the iris example, seaborn knows not to plot species, but here it doesn't?
我遇到了问题hue。我认为这会奏效,而且确实如此,但是有很多错误/警告代码,并且现有图是 3x3 而不是 2x2。在 iris 示例中,seaborn 知道不要绘制物种,但在这里却没有?
df['species'] = np.random.choice(2,150)
sns.pairplot(df, hue="species", size=2.5)
KeyError Traceback (most recent call last)
<ipython-input-148-cae05573d2d1> in <module>()
6
7 df['species'] = np.random.choice(2,150)
----> 8 sns.pairplot(df, hue="species", size=2.5)
9
10 sns.plt.show()
/Users/John/anaconda/lib/python2.7/site-packages/seaborn/linearmodels.pyc in pairplot(data, hue, hue_order, palette, vars, x_vars, y_vars, kind, diag_kind, markers, size, aspect, dropna, plot_kws, diag_kws, grid_kws)
1756 # Add a legend
1757 if hue is not None:
-> 1758 grid.add_legend()
1759
1760 return grid
/Users/John/anaconda/lib/python2.7/site-packages/seaborn/axisgrid.pyc in add_legend(self, legend_data, title, label_order)
47 # Draw a full-figure legend outside the grid
48 figlegend = plt.figlegend(handles, label_order, "center right",
---> 49 scatterpoints=1)
50 self._legend = figlegend
51 figlegend.set_title(title)
/Users/John/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.pyc in figlegend(handles, labels, loc, **kwargs)
662
663 """
--> 664 l = gcf().legend(handles, labels, loc, **kwargs)
665 draw_if_interactive()
666 return l
/Users/John/anaconda/lib/python2.7/site-packages/matplotlib/figure.pyc in legend(self, handles, labels, *args, **kwargs)
1193 .. plot:: mpl_examples/pylab_examples/figlegend_demo.py
1194 """
-> 1195 l = Legend(self, handles, labels, *args, **kwargs)
1196 self.legends.append(l)
1197 l._remove_method = lambda h: self.legends.remove(h)
/Users/John/anaconda/lib/python2.7/site-packages/matplotlib/legend.pyc in __init__(self, parent, handles, labels, loc, numpoints, markerscale, scatterpoints, scatteryoffsets, prop, fontsize, borderpad, labelspacing, handlelength, handleheight, handletextpad, borderaxespad, columnspacing, ncol, mode, fancybox, shadow, title, framealpha, bbox_to_anchor, bbox_transform, frameon, handler_map)
369
370 if framealpha is None:
--> 371 self.get_frame().set_alpha(rcParams["legend.framealpha"])
372 else:
373 self.get_frame().set_alpha(framealpha)
/Users/John/anaconda/lib/python2.7/site-packages/matplotlib/__init__.pyc in __getitem__(self, key)
844 except (ValueError, RuntimeError):
845 # force the issue
--> 846 warnings.warn(_rcparam_warn_str.format(key=repr(k),
847 value=repr(v),
848 func='__init__'))
KeyError: u'legend.framealpha'



