[英]Matplotlib errorbar fails to read a pandas data frame
I have two data frames in pandas format that I am trying to plot as values and error bars. 我有两个熊猫格式的数据框,试图将它们绘制为值和误差线。 But the python interface complains about some error I cannot understand.
但是python接口抱怨一些我无法理解的错误。 I have tested a colleague's almost the same code, and it appears that the fact that I run python 3.5 while he utilizes 2.7, is the source of the error.
我已经测试了一位同事几乎相同的代码,并且看来我在他使用2.7的同时运行python 3.5的事实是错误的根源。 Therefore, I did test his code on my computer (python 3.5) and I am getting the same error message.
因此,我确实在计算机(python 3.5)上测试了他的代码,并且得到了相同的错误消息。
Bellow is a subset of my troubling code: 贝娄是我麻烦的代码的子集:
"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.)'])
Bellow is the error message I receive: 波纹管是我收到的错误消息:
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
Thanks for the help 谢谢您的帮助
The problem is the discrepancy between the Pandas indexing and the indexing of Matplotlib internal functions. 问题是熊猫索引与Matplotlib内部函数的索引之间存在差异。 One way to resolve it -albeit not being elegant- is to create a dummy dataframe just for the plotting purpose.
解决它的一种方法(尽管不是很优雅)是创建一个仅用于绘图目的的虚拟数据框。 In your case:
在你的情况下:
mean_spruce_dummy = mean_spruce
mean_spruce_dummy.columns = np.arange(0, len(mean_spruce))
In principle, this discrepancy is solved in the newer version of Pandas. 原则上,此差异在较新版本的Pandas中得以解决。
I'm seeing a similar error in python 2.7. 我在python 2.7中看到类似的错误。 My solution is to access the underlying data directly.
我的解决方案是直接访问基础数据。 This should work for you
这应该适合你
x = mean_spruce['wvl'].values
y = mean_spruce['Tgt./Ref. %'].values
yerr = std_spruce['Rad. (Ref.)'].values
plt.errorbar(x, y yerr=yerr)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.