英文:
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)
这会生成如下图所示的图表:
当使用格式为[[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:
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)
输出:
英文:
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:
答案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()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论