我有一个使用以下代码的工作图:
import datetime as dt
import matplotlib.dates as mdate
import matplotlib.pyplot as plt
x = ['2016-06-20 21:00:02.313000', '2016-06-20 21:15:01.855000', '2016-06-20 21:30:01.690000', '2016-06-20 21:45:01.882000', '2016-06-20 22:00:02.005000', '2016-06-20 22:15:01.730000', '2016-06-20 22:30:01.479000', '2016-06-20 22:45:01.638000', '2016-06-20 23:00:01.886000', '2016-06-20 23:15:01.682000', '2016-06-20 23:30:01.653000', '2016-06-20 23:45:01.690000', '2016-06-21 00:00:02.196000', '2016-06-21 00:15:01.658000', '2016-06-21 00:30:01.514000', '2016-06-21 00:45:01.542000', '2016-06-21 01:00:01.291000', '2016-06-21 01:15:01.551000', '2016-06-21 01:30:01.439000', '2016-06-21 01:45:01.543000', '2016-06-21 02:00:01.449000', '2016-06-21 02:15:01.589000', '2016-06-21 02:30:01.555000', '2016-06-21 02:45:01.076000', '2016-06-21 03:00:01.588000', '2016-06-21 03:15:01.429000', '2016-06-21 03:30:01.029000', '2016-06-21 03:45:01.358000', '2016-06-21 04:00:01.460000', '2016-06-21 04:15:01.528000', '2016-06-21 04:30:01.366000', '2016-06-21 04:45:01.078000', '2016-06-21 05:00:01.541000', '2016-06-21 05:15:01.351000', '2016-06-21 05:30:01.618000', '2016-06-21 05:45:01.283000', '2016-06-21 06:00:01.297000', '2016-06-21 06:15:01.226000', '2016-06-21 06:30:01.219000', '2016-06-21 06:45:01.401000', '2016-06-21 07:00:01.410000', '2016-06-21 07:15:01.129000', '2016-06-21 07:30:01.420000', '2016-06-21 07:45:00.885000', '2016-06-21 08:00:01.338000', '2016-06-21 08:15:01.178000', '2016-06-21 08:30:01.148000', '2016-06-21 08:45:01.112000', '2016-06-21 09:00:01.357000', '2016-06-21 09:15:01.150000', '2016-06-21 09:30:00.814000', '2016-06-21 09:45:02.252000', '2016-06-21 10:00:01.757000', '2016-06-21 10:15:02.180000', '2016-06-21 10:30:02.188000', '2016-06-21 10:45:01.477000', '2016-06-21 11:00:02.100000', '2016-06-21 11:15:01.983000', '2016-06-21 11:30:02.038000', '2016-06-21 11:45:01.841000', '2016-06-21 12:00:01.825000', '2016-06-21 12:15:01.551000', '2016-06-21 12:30:01.716000', '2016-06-21 12:45:01.791000', '2016-06-21 13:00:02.297000', '2016-06-21 13:15:01.880000', '2016-06-21 13:30:01.433000', '2016-06-21 13:45:01.469000', '2016-06-21 14:00:01.769000', '2016-06-21 14:15:01.742000', '2016-06-21 14:30:01.803000', '2016-06-21 14:45:01.462000', '2016-06-21 15:00:01.819000', '2016-06-21 15:15:01.680000', '2016-06-21 15:30:01.657000', '2016-06-21 15:45:01.480000', '2016-06-21 16:00:01.578000', '2016-06-21 16:15:01.198000', '2016-06-21 16:30:01.584000', '2016-06-21 16:45:01.823000', '2016-06-21 17:00:01.636000', '2016-06-21 17:15:01.599000', '2016-06-21 17:30:01.419000', '2016-06-21 17:45:01.600000', '2016-06-21 18:00:01.746000', '2016-06-21 18:15:01.463000', '2016-06-21 18:30:01.345000', '2016-06-21 18:45:01.680000', '2016-06-21 19:00:01.509000', '2016-06-21 19:15:01.489000', '2016-06-21 19:30:01.708000', '2016-06-21 19:45:01.223000', '2016-06-21 20:00:01.310000', '2016-06-21 20:15:01.288000', '2016-06-21 20:30:01.188000', '2016-06-21 20:45:01.190000', '2016-06-21 21:00:01.260000', '2016-06-21 21:15:01.250000', '2016-06-21 21:30:01.218000', '2016-06-21 21:45:01.212000', '2016-06-21 22:00:01.220000', '2016-06-21 22:15:00.912000', '2016-06-21 22:30:00.840000', '2016-06-21 22:45:01.031000', '2016-06-21 23:00:01.159000', '2016-06-21 23:15:01.037000', '2016-06-21 23:30:00.851000', '2016-06-21 23:45:01.312000', '2016-06-22 00:00:02.867000', '2016-06-22 00:15:02.093000', '2016-06-22 00:30:02.074000', '2016-06-22 00:45:01.943000', '2016-06-22 01:00:02.038000', '2016-06-22 01:15:02.057000', '2016-06-22 01:30:01.854000', '2016-06-22 01:45:02.364000', '2016-06-22 02:00:01.976000', '2016-06-22 02:15:01.887000', '2016-06-22 02:30:01.720000', '2016-06-22 02:45:01.736000', '2016-06-22 03:00:01.901000', '2016-06-22 03:15:01.872000', '2016-06-22 03:30:01.457000', '2016-06-22 03:45:01.524000', '2016-06-22 04:00:02.015000', '2016-06-22 04:15:01.838000', '2016-06-22 04:30:01.637000', '2016-06-22 04:45:01.730000', '2016-06-22 05:00:01.781000', '2016-06-22 05:15:01.999000', '2016-06-22 05:30:01.508000', '2016-06-22 05:45:01.532000', '2016-06-22 06:00:01.100000', '2016-06-22 06:15:01.263000', '2016-06-22 06:30:01.267000', '2016-06-22 06:45:01.611000', '2016-06-22 07:00:01.718000', '2016-06-22 07:15:01.512000', '2016-06-22 07:30:01.485000', '2016-06-22 07:45:01.618000', '2016-06-22 08:00:01.665000', '2016-06-22 08:15:01.347000', '2016-06-22 08:30:01.294000', '2016-06-22 08:45:01.345000', '2016-06-22 09:00:01.599000', '2016-06-22 09:15:01.481000', '2016-06-22 09:30:01.219000', '2016-06-22 09:45:01.099000', '2016-06-22 10:00:01.318000', '2016-06-22 10:15:00.792000', '2016-06-22 10:30:01.341000', '2016-06-22 10:45:01.680000', '2016-06-22 11:00:01.608000', '2016-06-22 11:15:01.496000', '2016-06-22 11:30:01.235000', '2016-06-22 11:45:01.428000', '2016-06-22 12:00:01.384000', '2016-06-22 12:15:01.136000', '2016-06-22 12:30:01.691000', '2016-06-22 12:45:01.247000', '2016-06-22 13:00:01.511000', '2016-06-22 13:15:00.956000', '2016-06-22 13:30:01.055000', '2016-06-22 13:45:01.042000', '2016-06-22 14:00:01.314000', '2016-06-22 14:15:01.211000', '2016-06-22 14:30:01.619000', '2016-06-22 14:45:01.990000', '2016-06-22 15:00:02.147000', '2016-06-22 15:15:01.906000', '2016-06-22 15:30:01.873000', '2016-06-22 15:45:02.065000', '2016-06-22 16:00:01.892000', '2016-06-22 16:15:01.823000', '2016-06-22 16:30:01.950000', '2016-06-22 16:45:01.871000', '2016-06-22 17:00:01.638000', '2016-06-22 17:15:01.664000', '2016-06-22 17:30:01.897000', '2016-06-22 17:45:01.862000', '2016-06-22 18:00:01.638000', '2016-06-22 18:15:01.700000', '2016-06-22 18:30:01.610000', '2016-06-22 18:45:01.589000', '2016-06-22 19:00:01.876000', '2016-06-22 19:15:01.849000', '2016-06-22 19:30:02.241000', '2016-06-22 19:45:01.752000', '2016-06-22 20:00:01.305000', '2016-06-22 20:15:01.584000', '2016-06-22 20:30:01.479000', '2016-06-22 20:45:01.448000', '2016-06-22 21:00:01.714000', '2016-06-22 21:15:01.626000', '2016-06-22 21:30:01.545000', '2016-06-22 21:45:01.426000', '2016-06-22 22:00:01.656000', '2016-06-22 22:15:01.653000', '2016-06-22 22:30:01.396000', '2016-06-22 22:45:01.529000', '2016-06-22 23:00:01.463000', '2016-06-22 23:15:00.991000', '2016-06-22 23:30:01.371000', '2016-06-22 23:45:01.804000', '2016-06-23 00:00:02.017000', '2016-06-23 00:15:01.199000', '2016-06-23 00:30:01.319000', '2016-06-23 00:45:01.383000', '2016-06-23 01:00:01.298000', '2016-06-23 01:15:01.327000', '2016-06-23 01:30:01.576000', '2016-06-23 01:45:01.166000', '2016-06-23 02:00:01.206000', '2016-06-23 02:15:01.202000', '2016-06-23 02:30:01.172000', '2016-06-23 02:45:01.088000', '2016-06-23 03:00:01.239000', '2016-06-23 03:15:01.062000', '2016-06-23 03:30:00.924000', '2016-06-23 03:45:01.009000', '2016-06-23 04:00:00.732000', '2016-06-23 04:15:01.003000', '2016-06-23 04:30:01.136000', '2016-06-23 04:45:02.176000', '2016-06-23 05:00:02.134000', '2016-06-23 05:15:02.031000', '2016-06-23 05:30:01.832000', '2016-06-23 05:45:02.024000', '2016-06-23 06:00:02.323000', '2016-06-23 06:15:02.567000', '2016-06-23 06:30:02.139000', '2016-06-23 06:45:01.507000', '2016-06-23 07:15:01.507000', '2016-06-23 07:30:01.896000', '2016-06-23 07:45:01.886000', '2016-06-23 08:00:01.877000', '2016-06-23 08:15:01.806000', '2016-06-23 08:30:02.031000', '2016-06-23 08:45:01.692000', '2016-06-23 09:00:02.013000', '2016-06-23 09:15:01.888000', '2016-06-23 09:30:01.843000', '2016-06-23 09:45:01.733000', '2016-06-23 10:00:03.221000', '2016-06-23 10:15:01.600000', '2016-06-23 10:30:01.751000', '2016-06-23 10:45:01.262000', '2016-06-23 11:00:01.942000', '2016-06-23 11:15:01.619000', '2016-06-23 11:30:01.574000', '2016-06-23 11:45:01.471000', '2016-06-23 12:00:01.638000', '2016-06-23 12:15:01.490000', '2016-06-23 12:30:01.296000', '2016-06-23 12:45:01.407000', '2016-06-23 13:00:01.690000', '2016-06-23 13:15:01.665000', '2016-06-23 13:30:01.470000', '2016-06-23 13:45:00.896000', '2016-06-23 14:00:01.407000', '2016-06-23 14:15:01.305000', '2016-06-23 14:30:01.342000', '2016-06-23 14:45:01.020000', '2016-06-23 15:00:01.278000', '2016-06-23 15:15:01.257000', '2016-06-23 15:30:01.489000', '2016-06-23 15:45:01.160000', '2016-06-23 16:00:01.278000', '2016-06-23 16:15:01.228000', '2016-06-23 16:30:01.347000', '2016-06-23 16:45:01.477000', '2016-06-23 17:00:01.295000', '2016-06-23 17:15:01.093000', '2016-06-23 17:30:01.524000', '2016-06-23 17:45:01.216000', '2016-06-23 18:00:01.129000', '2016-06-23 18:15:01.251000', '2016-06-23 18:30:01.524000', '2016-06-23 18:45:01.057000', '2016-06-23 19:00:00.575000', '2016-06-23 19:15:02.286000', '2016-06-23 19:30:02.305000', '2016-06-23 19:45:01.905000', '2016-06-23 20:00:02.355000', '2016-06-23 20:15:01.615000', '2016-06-23 20:30:01.801000', '2016-06-23 20:45:01.946000', '2016-06-23 21:00:02.196000', '2016-06-23 21:15:01.927000', '2016-06-23 21:30:01.561000', '2016-06-23 21:45:01.830000', '2016-06-23 22:00:01.954000', '2016-06-23 22:15:01.548000', '2016-06-23 22:30:01.778000', '2016-06-23 22:45:01.536000', '2016-06-23 23:00:01.802000', '2016-06-23 23:15:01.827000', '2016-06-23 23:30:01.265000', '2016-06-23 23:45:01.953000', '2016-06-24 00:00:02.483000', '2016-06-24 00:15:01.594000', '2016-06-24 00:30:01.616000', '2016-06-24 00:45:01.727000', '2016-06-24 01:00:01.853000', '2016-06-24 01:15:01.645000', '2016-06-24 01:30:01.653000', '2016-06-24 01:45:01.201000', '2016-06-24 02:00:01.230000', '2016-06-24 02:15:01.400000', '2016-06-24 02:30:01.477000', '2016-06-24 02:45:01.649000', '2016-06-24 03:00:01.582000', '2016-06-24 03:15:01.481000', '2016-06-24 03:30:01.397000', '2016-06-24 03:45:01.272000', '2016-06-24 04:00:01.487000', '2016-06-24 04:15:01.429000', '2016-06-24 04:30:01.328000', '2016-06-24 04:45:01.348000', '2016-06-24 05:00:01.393000', '2016-06-24 05:15:00.969000', '2016-06-24 05:30:01.529000', '2016-06-24 05:45:00.857000', '2016-06-24 06:00:01.117000', '2016-06-24 06:15:01.230000', '2016-06-24 06:30:01.378000', '2016-06-24 06:45:00.877000', '2016-06-24 07:00:01.310000', '2016-06-24 07:15:01.167000']
y = ['29.99', '30', '30', '29.99', '30', '30.02', '30.02', '30.03', '30.04', '30.05', '30.06', '30.06', '30.06', '30.05', '30.05', '30.05', '30.05', '30.05', '30.05', '30.05', '30.04', '30.04', '30.04', '30.04', '30.04', '30.04', '30.04', '30.04', '30.04', '30.03', '30.03', '30.03', '30.03', '30.03', '30.03', '30.03', '30.04', '30.04', '30.04', '30.04', '30.04', '30.04', '30.04', '30.04', '30.05', '30.05', '30.05', '30.05', '30.05', '30.04', '30.04', '30.05', '30.05', '30.05', '30.04', '30.04', '30.04', '30.04', '30.04', '30.04', '30.04', '30.04', '30.04', '30.03', '30.03', '30.02', '30.02', '30.01', '30.01', '30.01', '30', '29.99', '29.99', '29.98', '29.98', '29.97', '29.97', '29.96', '29.96', '29.96', '29.95', '29.94', '29.94', '29.94', '29.93', '29.92', '29.92', '29.92', '29.92', '29.92', '29.92', '29.92', '29.92', '29.93', '29.93', '29.93', '29.93', '29.93', '29.93', '29.94', '29.94', '29.94', '29.94', '29.94', '29.94', '29.94', '29.94', '29.94', '29.95', '29.95', '29.95', '29.95', '29.94', '29.94', '29.94', '29.94', '29.93', '29.92', '29.9', '29.9', '29.91', '29.93', '29.95', '29.95', '29.96', '29.95', '29.94', '29.94', '29.96', '29.96', '29.96', '29.95', '29.95', '29.99', '29.99', '29.97', '29.97', '30', '29.99', '29.99', '29.99', '29.98', '29.92', '29.9', '29.9', '29.89', '29.91', '29.91', '29.91', '29.92', '29.92', '29.92', '29.92', '29.96', '29.99', '30.04', '30.04', '30.02', '29.89', '29.89', '29.89', '29.89', '29.91', '29.9', '29.91', '29.91', '29.91', '29.89', '29.89', '29.89', '29.89', '29.89', '29.88', '29.88', '29.88', '29.87', '29.87', '29.86', '29.86', '29.86', '29.84', '29.83', '29.83', '29.82', '29.82', '29.8', '29.8', '29.78', '29.76', '29.77', '29.77', '29.75', '29.74', '29.74', '29.73', '29.73', '29.76', '29.83', '29.82', '29.82', '29.79', '29.78', '29.76', '29.76', '29.76', '29.78', '29.77', '29.77', '29.78', '29.78', '29.78', '29.78', '29.78', '29.75', '29.77', '29.77', '29.77', '29.77', '29.78', '29.79', '29.79', '29.79', '29.79', '29.79', '29.79', '29.8', '29.8', '29.8', '29.8', '29.81', '29.81', '29.81', '29.82', '29.82', '29.83', '29.83', '29.84', '29.84', '29.85', '29.85', '29.86', '29.87', '29.87', '29.87', '29.87', '29.87', '29.87', '29.88', '29.88', '29.88', '29.88', '29.88', '29.89', '29.89', '29.89', '29.89', '29.89', '29.89', '29.89', '29.89', '29.89', '29.89', '29.89', '29.9', '29.9', '29.9', '29.9', '29.9', '29.89', '29.89', '29.89', '29.89', '29.89', '29.89', '29.9', '29.89', '29.89', '29.89', '29.89', '29.9', '29.9', '29.9', '29.9', '29.92', '29.92', '29.92', '29.92', '29.92', '29.94', '29.94', '29.94', '29.95', '29.95', '29.96', '29.96', '29.97', '29.97', '29.98', '29.98', '29.98', '29.99', '29.99', '29.99', '30.0', '30.0', '30.0', '30.0', '30.0', '30.0', '30.0', '30.0', '30.0', '30.0', '29.99', '29.99', '29.99', '29.99', '29.99', '29.99', '29.99', '30.01', '30.01', '30.02', '30.02', '30.03', '30.03', '30.04', '30.04', '30.04']
list_of_dates = [dt.datetime.strptime(obs, '%Y-%m-%d %H:%M:%S.%f') for obs in x]
dates_to_plot = mdate.date2num(list_of_dates)
fig1 = plt.figure(1, figsize=(7, 3))
ax = fig1.add_subplot(111)
ax.plot(dates_to_plot, y)
ax.xaxis.set_major_formatter(mdate.DateFormatter('%a'))
plt.show()
但当我将ax.plot
更改为ax.bar
时,Matplotlib会抛出错误说:
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/matplotlib/transforms.py:745:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
我意识到将y obs转换为浮动将清除错误,我只是好奇为什么(其他所有相等)ax.plot可以将数据作为字符串处理,但ax.bar不能。