【问题标题】:Having trouble with seaborn module in pythonpython中的seaborn模块遇到问题
【发布时间】:2016-05-12 20:10:26
【问题描述】:

我正在尝试使用 seaborn 的 jointplot() 方法绘制一些基本图。

我的 pandas 数据框如下所示:

Out[250]:
YEAR    Yields  avgSumPcpn  avgMaxSumTemp   avgMinSumTemp
1970    5000    133.924981  30.437124   19.026974
1971    5560    107.691316  31.161974   19.278186
1972    5196    116.830066  31.454192   19.443712
1973    4233    181.550733  30.373581   19.097679
1975    5093    112.137538  30.428966   18.863224

我正在尝试将'Yields''YEAR' 进行对比(因此,可以通过绘图查看'Yields' 如何随时间变化)。一个简单的情节。

但是当我这样做时:

sns.jointplot(x='YEAR',y='Yeilds', data = summer_pcpn_temp_yeild, kind = 'reg', size = 10)

我收到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-251-587582a746b8> in <module>()
      3 #ax = plt.axes()
      4 #sns_sum_reg_min_temp_pcpn = sns.regplot(x='avgSumPcpn',y='avgMaxSumTemp', data = df_sum_temp_pcpn)
----> 5 sns.jointplot(x='Yeilds',y='YEAR', data = summer_pcpn_temp_yeild, kind = 'reg', size = 10)
      6 plt.title('Avg Summer Precipitation vs Yields of Wharton TX', fontsize = 10)
      7 

//anaconda/lib/python2.7/site-packages/seaborn/distributions.pyc in jointplot(x, y, data, kind, stat_func, color, size, ratio, space, dropna, xlim, ylim, joint_kws, marginal_kws, annot_kws, **kwargs)
    793     grid = JointGrid(x, y, data, dropna=dropna,
    794                      size=size, ratio=ratio, space=space,
--> 795                      xlim=xlim, ylim=ylim)
    796 
    797     # Plot the data using the grid

//anaconda/lib/python2.7/site-packages/seaborn/axisgrid.pyc in __init__(self, x, y, data, size, ratio, space, dropna, xlim, ylim)
   1637         if dropna:
   1638             not_na = pd.notnull(x) & pd.notnull(y)
-> 1639             x = x[not_na]
   1640             y = y[not_na]
   1641 

TypeError: string indices must be integers, not Series

所以我打印出每一列的类型。方法如下:

for i in summer_pcpn_temp_yeild.columns.values.tolist():
    print type(summer_pcpn_temp_yeild[[i]])

print type(summer_pcpn_temp_yeild.index.values)

这给了我:

<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<type 'numpy.ndarray'>

所以,我无法理解如何解决它。

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: python python-2.7 pandas seaborn


    【解决方案1】:

    检查 YEARYields 是否具有 integer(不是 string)类型的值。

    【讨论】:

      【解决方案2】:

      尝试在对jointplot 的调用中将x='Yields' 更改为x='Yields':

      sns.jointplot(x='YEAR',y='Yeilds', data = summer_pcpn_temp_yeild, kind = 'reg', size = 10)
      

      错误信息具有误导性。 Seaborn 在您的 summer_pcpn_temp_yeild 数据框中找不到名为“Yields”的列,因为该数据框列拼写为“Yields”。

      我遇到了同样的问题,并通过将 x= 参数更正为 sns.jointplot() 来修复它

      【讨论】:

        猜你喜欢
        • 2020-10-19
        • 2010-12-29
        • 1970-01-01
        • 2019-02-23
        • 1970-01-01
        • 1970-01-01
        • 2013-06-14
        • 2011-01-17
        • 2012-01-07
        相关资源
        最近更新 更多