Python Matplotlib errorbar无法读取数据帧

Python Matplotlib errorbar无法读取数据帧,python,python-3.x,pandas,matplotlib,Python,Python 3.x,Pandas,Matplotlib,我有两个熊猫格式的数据帧,我试图将其绘制为值和误差条。但是python接口抱怨一些我无法理解的错误。我已经测试了一位同事几乎相同的代码,而我在他使用2.7时运行Python3.5似乎是错误的根源。因此,我在我的计算机(Python3.5)上测试了他的代码,得到了相同的错误消息 下面是我的麻烦代码的一个子集: "Using pandas library to combine the three white spruce data sets" trees = [white_spruce_1,whit

我有两个熊猫格式的数据帧,我试图将其绘制为值和误差条。但是python接口抱怨一些我无法理解的错误。我已经测试了一位同事几乎相同的代码,而我在他使用2.7时运行Python3.5似乎是错误的根源。因此,我在我的计算机(Python3.5)上测试了他的代码,得到了相同的错误消息

下面是我的麻烦代码的一个子集:

"Using pandas library to combine the three white spruce data sets"
trees = [white_spruce_1,white_spruce_2,white_spruce_3]
ntrees = pd.concat(trees) # Concatenate list into a series
spruce_stat = ntrees.groupby("Wvl") #Converted the series into a panda object

mean_spruce = spruce_stat.mean()
std_spruce = spruce_stat.std()
#mean_spruce.head()
mean_spruce['wvl']=mean_spruce.index
mean_spruce.head()

     Chan.# Rad. (Ref.) Rad. (Target)   Tgt./Ref. %
Wvl             
350     0        0       0.000014        0.686176
351     0        0       0.000015        0.707577

std_spruce.head()

       Chan.#   Rad. (Ref.) Rad. (Target)   Tgt./Ref. %
Wvl             
350       0       0          0.000014        0.686176
351       0       0          0.000015        0.707577

plt.errorbar(mean_spruce['wvl'],mean_spruce['Tgt./Ref. %'], xerr = None, yerr = std_spruce['Rad. (Ref.)'])
下面是我收到的错误消息:

KeyError                Traceback (most recent call last)
<ipython-input-52-13352d94b09c> in <module>()
      2 #plt.errorbar(mean_spruce['wvl'],mean_spruce['Tgt./Ref. %'], xerr = None,yerr=std_spruce['Tgt./Ref. %'],c='k',ecolor='r', elinewidth=0.5, errorevery=5)
      3 #plt.errorbar( x, y, xerr = None , yerr = sd_white_spruce['Tgt./Ref. %'],c = 'green', ecolor = 'red', capsize = 0,elinewidth = 0.5, errorevery = 5 )
----> 4 plt.errorbar(mean_spruce['wvl'],mean_spruce['Tgt./Ref. %'], xerr = None, yerr = std_spruce['Rad. (Ref.)'])# ,c = 'green', ecolor = 'red', capsize = 0,elinewidth = 0.5, errorevery = 5)
      5 

C:\Users\mike\Anaconda3\lib\site-packages\matplotlib\pyplot.py in errorbar(x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, hold, data, **kwargs)
   2828                           xlolims=xlolims, xuplims=xuplims,
   2829                           errorevery=errorevery, capthick=capthick, data=data,
-> 2830                           **kwargs)
   2831     finally:
   2832         ax.hold(washold)

C:\Users\mike\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
   1809                     warnings.warn(msg % (label_namer, func.__name__),
   1810                                   RuntimeWarning, stacklevel=2)
-> 1811             return func(ax, *args, **kwargs)
   1812         pre_doc = inner.__doc__
   1813         if pre_doc is None:

C:\Users\mike\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in errorbar(self, x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, **kwargs)
   2961                 # Check for scalar or symmetric, as in xerr.
   2962                 if len(yerr) > 1 and not ((len(yerr) == len(y) and not (
-> 2963                         iterable(yerr[0]) and len(yerr[0]) > 1))):
   2964                     raise ValueError("yerr must be a scalar, the same "
   2965                                      "dimensions as y, or 2xN.")

C:\Users\mike\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    555     def __getitem__(self, key):
    556         try:
--> 557             result = self.index.get_value(self, key)
    558 
    559             if not np.isscalar(result):

C:\Users\mike\Anaconda3\lib\site-packages\pandas\core\index.py in get_value(self, series, key)
   3882 
   3883         k = _values_from_object(key)
-> 3884         loc = self.get_loc(k)
   3885         new_values = _values_from_object(series)[loc]
   3886 

C:\Users\mike\Anaconda3\lib\site-packages\pandas\core\index.py in get_loc(self, key, method, tolerance)
   3940             pass
   3941         return super(Float64Index, self).get_loc(key, method=method,
-> 3942                                                  tolerance=tolerance)
   3943 
   3944     @property

C:\Users\mike\Anaconda3\lib\site-packages\pandas\core\index.py in get_loc(self, key, method, tolerance)
   1757                                  'backfill or nearest lookups')
   1758             key = _values_from_object(key)
-> 1759             return self._engine.get_loc(key)
   1760 
   1761         indexer = self.get_indexer([key], method=method,

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:3979)()

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:3843)()

pandas\hashtable.pyx in pandas.hashtable.Float64HashTable.get_item (pandas\hashtable.c:9556)()

pandas\hashtable.pyx in pandas.hashtable.Float64HashTable.get_item (pandas\hashtable.c:9494)()

KeyError: 0.0
keyrerror回溯(最近一次调用)
在()
2#plt.errorbar(平均云杉['wvl'],平均云杉['Tgt./Ref.%',xerr=None,yerr=std\U云杉['Tgt./Ref.%',c='k',ecolor='r',elinewidth=0.5,errorevery=5)
3#plt.errorbar(x,y,xerr=None,yerr=sd#u white#u Clouds['Tgt./Ref.%',c='green',ecocolor='red',capsize=0,elinewidth=0.5,errorevery=5)
---->4 plt.errorbar(平均云杉['wvl'],平均云杉['Tgt./Ref.%],XER=None,YER=std\U云杉['Rad.(Ref.)”)))#,c='green',ecolor='red',衣壳=0,elinewidth=0.5,errorevery=5)
5.
C:\Users\mike\Anaconda3\lib\site packages\matplotlib\pyplot.py在errorbar中(x、y、YER、XER、fmt、ecolor、elinewidth、翻船、barsabove、lolims、uplims、xlolims、xuplims、errorevery、capthick、hold、data、**kwargs)
2828 xlolims=xlolims,xuplims=xuplims,
2829 errorevery=errorevery,capthick=capthick,data=data,
->2830**夸尔格)
2831最后:
2832斧头保持(洗旧)
C:\Users\mike\Anaconda3\lib\site packages\matplotlib\\uuuuu init\uuuuuuuuu.py在内部(ax,*args,**kwargs)
1809警告。警告(消息%(标签名称,功能名称),
1810运行时警告,堆栈级别=2)
->1811返回函数(ax,*args,**kwargs)
1812预付款单=内部付款单__
1813如果pre_doc为无:
C:\Users\mike\Anaconda3\lib\site packages\matplotlib\axes\\u axes.py在errorbar中(self、x、y、yerr、xerr、fmt、ecolor、elinewidth、captsabove、lolims、uplims、xlolims、xuplims、errorevery、capthick、**kwargs)
2961#检查标量或对称,如XER。
2962如果len(yerr)>1而不是((len(yerr)==len(y)而不是(
->2963 iterable(yerr[0])和len(yerr[0])>1)):
2964 raise VALUERROR(“YER必须是标量,相同”
2965“尺寸为y或2xN。”)
C:\Users\mike\Anaconda3\lib\site packages\pandas\core\series.py in\uuuuu getitem\uuuuuuuu(self,key)
555 def_uuugetItem_uuuu(自身,密钥):
556尝试:
-->557结果=self.index.get_值(self,key)
558
559如果不是np.isscalar(结果):
C:\Users\mike\Anaconda3\lib\site packages\pandas\core\index.py in get_value(self、series、key)
3882
3883 k=_对象(键)中的_值
->3884 loc=自身获取loc(k)
3885新的_值=_对象(系列)的_值[loc]
3886
C:\Users\mike\Anaconda3\lib\site packages\pandas\core\index.py in get\u loc(self、key、method、tolerance)
3940通行证
3941返回super(Float64Index,self).get_loc(key,method=method,
->3942公差=公差)
3943
3944@property
C:\Users\mike\Anaconda3\lib\site packages\pandas\core\index.py in get\u loc(self、key、method、tolerance)
1757“回填或最近的查找”)
1758键=来自对象(键)的值
->1759返回发动机。获取位置(钥匙)
1760
1761 indexer=self.get\u indexer([key],method=method,
pandas.index.IndexEngine.get_loc中的pandas\index.pyx(pandas\index.c:3979)()
pandas.index.IndexEngine.get_loc中的pandas\index.pyx(pandas\index.c:3843)()
pandas.hashtable.Float64HashTable.get_项中的pandas\hashtable.pyx(pandas\hashtable.c:9556)()
pandas.hashtable.Float64HashTable.get_项中的pandas\hashtable.pyx(pandas\hashtable.c:9494)()
KeyError:0.0

感谢您的帮助

问题在于Pandas索引和Matplotlib内部函数的索引之间存在差异。解决此问题的一种方法是创建一个仅用于打印目的的虚拟数据框。在您的情况下:

mean_spruce_dummy = mean_spruce
mean_spruce_dummy.columns = np.arange(0, len(mean_spruce))

原则上,新版本的Pandas解决了这个问题。

问题在于Pandas索引和Matplotlib内部函数索引之间的差异。解决这个问题的一种方法是创建一个仅用于打印的虚拟数据框。在您的情况下:

mean_spruce_dummy = mean_spruce
mean_spruce_dummy.columns = np.arange(0, len(mean_spruce))

原则上,这种差异在新版本的Pandas中得到了解决。

我在python 2.7中看到了一个类似的错误。我的解决方案是直接访问底层数据。这应该适用于您

x = mean_spruce['wvl'].values
y = mean_spruce['Tgt./Ref. %'].values
yerr = std_spruce['Rad. (Ref.)'].values

plt.errorbar(x, y yerr=yerr) 

我在Python2.7中看到了一个类似的错误。我的解决方案是直接访问底层数据。这应该适合您

x = mean_spruce['wvl'].values
y = mean_spruce['Tgt./Ref. %'].values
yerr = std_spruce['Rad. (Ref.)'].values

plt.errorbar(x, y yerr=yerr) 

好吧,解释这个错误很容易——在某一点上,值
0.0
被用作键(我假设是df),这是不允许的,因为它是一个浮点,而不是一个int。例如,你不能为第1.7行编制索引。我仍然不清楚,在我的代码中有一个0.0键,叫做std_struce[Tgt./Ref.%]在我的朋友计算机上,代码没有很好的值。代码很好,我不知道这是否能解决这个问题,但是请考虑使用<代码> MythySpRe==mean_spruce.index@Arcticpython如果代码在另一台计算机上运行,那么熊猫本身可能有问题。您在每台计算机上运行的是哪个版本(
pd.\uuuuuu version\uuuuuuu
)?如果可以,我建议更新到0.17.1。我正在运行0.17.1。我的代码和我的朋友的代码似乎不起作用