如何在 datetime.datetime 坐标轴上显示误差线?

huangapple go评论64阅读模式
英文:

How can I display error bars an a datetime.datetime axis?

问题

我正在尝试在时间轴上制作带有可调整的上/下误差限的图表。我一直在使用datetime.datetime数据类型来实现这一点,这在使用matplotlib.pyplot.errorbar()尝试实现误差棒时一直可靠。

import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np

dates = [datetime(1950, 2, 2, 12), datetime(1950, 2, 2, 15)]
data = [1, 2]
date_err = [[datetime(1950, 2, 2, 11, 30), datetime(1950, 2, 2, 14, 30)],
            [datetime(1950, 2, 2, 12, 30), datetime(1950, 2, 2, 15, 30)]]
test_err = [2, 3]
fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.plot(dates, data, 'k.')
ax1.tick_params(rotation=45)
plt.subplots_adjust(hspace=0.5)
ax2.errorbar(dates, data, xerr=test_err)  # 我尝试过date_err和test_err
ax2.errorbar(dates, data, xerr=date_err)

这会生成如下图所示的图表:

如何在 datetime.datetime 坐标轴上显示误差线?

当使用格式为[[low1, low2...lowN], [high1, high2...highN]]的形状为(2,N)的列表,数据类型为datetime.datetime()时,会出现以下错误:

TypeError: unsupported operand type(s) for *: 'int' and 'datetime.datetime'

而在使用整数/浮点数(例如test_err中的数据)时,会出现以下错误:

TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'int'

是否有人可以建议一种使用日期/时间绘制图表的替代方法,或者一种在时间轴上实现误差棒的方法?谢谢!
1: https://i.stack.imgur.com/E9bgE.png

英文:

I'm trying to make a plot with error bars along the time axis, ideally with adjustable upper/lower error limits. I've been using the datetime.datetime datatype to do this, which has been reliable until I've tried to implement error bars with matplotlib.pyplot.errorbar().

import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np

dates = [datetime(1950,2,2,12),datetime(1950,2,2,15)]
data = [1,2]
date_err = [[datetime(1950,2,2,11,30),datetime(1950,2,2,14,30)], #arbitrary random dates/times
            [datetime(1950,2,2,12,30),datetime(1950,2,2,15,30)]]
test_err = [2,3]
fig, (ax1,ax2) = plt.subplots(2,1)
ax1.plot(dates,data,'k.')
ax1.tick_params(rotation = 45)
plt.subplots_adjust(hspace = 0.5)
ax2.errorbar(dates,data,xerr = test_err) #I tried both with date_err and test_err
ax2.errorbar(dates,data,xerr = date_err)

which gives the plot:

如何在 datetime.datetime 坐标轴上显示误差线?

I tried both using a list shape (2,N) with format [[low1,low2...lowN],[high1,high2...highN]] with datatype datetime.datetime() but I recieve the error:

TypeError: unsupported operand type(s) for *: 'int' and 'datetime.datetime'

When using integers/floating point numbers like in test_err, I get the error:

TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'int'

Can anyone either suggest an alternative way to plot with dates/times or a way to implement error bars along the temporal axis? Thank you!

答案1

得分: 2

我猜你想要 timedelta

from datetime import datetime, timedelta

test_err = [timedelta(hours=2), timedelta(hours=3)]
ax2.errorbar(dates, data, xerr=test_err)

输出:

如何在 datetime.datetime 坐标轴上显示误差线?

英文:

I'm guessing you want timedelta:

from datetime import datetime, timedelta

test_err = [timedelta(hours=2), timedelta(hours=3)]
ax2.errorbar(dates, data, xerr=test_err)

Output:

如何在 datetime.datetime 坐标轴上显示误差线?

答案2

得分: 2

你可以将 timedelta 用作错误。我不知道你想要的错误是什么,但你可以轻松调整它。

import matplotlib.pyplot as plt
from datetime import datetime, timedelta

plt.close("all")

dates = [datetime(1950, 2, 2, 12), datetime(1950, 2, 2, 15)]
data = [1, 2]
test_err = [timedelta(minutes=30), timedelta(hours=2)]

fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.plot(dates, data, 'k.')
ax2.errorbar(dates, data, xerr=test_err, capsize=3)
ax1.tick_params(rotation=45)
ax2.tick_params(rotation=45)
fig.tight_layout()
fig.show()

如何在 datetime.datetime 坐标轴上显示误差线?

英文:

You can use timedelta as the errors. I don't know what your desired errors are, but you can easily adjust that.

import matplotlib.pyplot as plt
from datetime import datetime, timedelta

plt.close("all")

dates = [datetime(1950, 2, 2, 12), datetime(1950, 2, 2, 15)]
data = [1, 2]
test_err = [timedelta(minutes=30), timedelta(hours=2)]

fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.plot(dates, data, 'k.')
ax2.errorbar(dates, data, xerr=test_err, capsize=3)
ax1.tick_params(rotation=45)
ax2.tick_params(rotation=45)
fig.tight_layout()
fig.show()

如何在 datetime.datetime 坐标轴上显示误差线?

huangapple
  • 本文由 发表于 2023年6月9日 01:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定