针对日期时间的错误栏图失败 [英] errorbar plot against datetime fails

查看:463
本文介绍了针对日期时间的错误栏图失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

x轴为datetime时,误差线图并不总是起作用:

Errorbar plotting does not always work when the x axis is a datetime:

z = pd.DataFrame({'timestamp': {0: pd.Timestamp('2018-06-16 04:33:27'),
  1: pd.Timestamp('2018-06-16 18:07:40')},
 'average': {0: 1.4158309812874796, 1: 1.4293226152856995},
 'stdev': {0: 0.5721450460404708, 1: 0.5771658975429514}})

现在z

            timestamp   average     stdev
0 2018-06-16 04:33:27  1.415831  0.572145
1 2018-06-16 18:07:40  1.429323  0.577166

plt.plot(z.timestamp, z.average)可以正常工作, 但是plt.errorbar(z.timestamp, z.average, yerr=z.stdev)产生

plt.plot(z.timestamp, z.average) works as expected, but plt.errorbar(z.timestamp, z.average, yerr=z.stdev) produces

<ErrorbarContainer object of 3 artists>
Error in callback <function install_repl_displayhook.<locals>.post_execute at 0x7fe251344840> (for post_execute):


Truncated Traceback (Use C-c C-x to view full TB):
~/.virtualenvs/algorisk/local/lib64/python3.6/site-packages/matplotlib/dates.py in viewlim_to_dt(self)
   1024                              'often happens if you pass a non-datetime '
   1025                              'value to an axis that has datetime units'
-> 1026                              .format(vmin))
   1027         return num2date(vmin, self.tz), num2date(vmax, self.tz)
   1028 

ValueError: view limit minimum -7.64586229992263e+16 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units

我在做什么错了?

PS.其他值似乎起作用.例如

PS. Other values seem to work. E.g.,

plt.errorbar(np.array([datetime.datetime(2018,7,30,12),
            datetime.datetime(2018,7,30,15)]).astype("datetime64[h]"),
         np.array([2,3]),
         yerr=np.array([1,2]))

按预期工作.

推荐答案

从本质上讲,matplotlib可以绘制numpy数组.与其提供熊猫Series到绘图功能,您可能不希望提供通过.values获得的底层numpy数组.

Natively, matplotlib can plot numpy arrays. Instead of providing pandas Series to the plotting function you probably want to provide the underlying numpy arrays, obtained via .values.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'timestamp': {0: pd.Timestamp('2018-06-16 04:33:27'),
                                1: pd.Timestamp('2018-06-16 18:07:40')},
                  'average': {0: 1.4158309812874796, 1: 1.4293226152856995},
                  'stdev': {0: 0.5721450460404708, 1: 0.5771658975429514}})

plt.errorbar(df.timestamp.values, df.average.values, yerr=df.stdev.values)

plt.show()

这篇关于针对日期时间的错误栏图失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆