如何为Python线图Bokeh/matplotlib datetime和图形设置datetime格式

如何为Python线图Bokeh/matplotlib datetime和图形设置datetime格式,python,datetime,matplotlib,bokeh,Python,Datetime,Matplotlib,Bokeh,我在构建我的第一个折线图时遇到了一些重大问题,目前我已经花了很长时间研究我的错误,用尽了所有的努力 我有以下日期时间示例数据,销售数据格式如下。正在从我的MySQL数据库导入数据 2015-12-30 01:58:00 10 2015-12-30 01:59:00 16 2015-12-30 02:00:00 21 2015-12-30 02:01:00 5 2015-12-30 02:02:00 2 2015-12-30 02:03:00 4 2015-12-30 02:04:00 11 20

我在构建我的第一个折线图时遇到了一些重大问题,目前我已经花了很长时间研究我的错误,用尽了所有的努力

我有以下日期时间示例数据,销售数据格式如下。正在从我的MySQL数据库导入数据

2015-12-30 01:58:00 10
2015-12-30 01:59:00 16
2015-12-30 02:00:00 21
2015-12-30 02:01:00 5
2015-12-30 02:02:00 2
2015-12-30 02:03:00 4
2015-12-30 02:04:00 11
2015-12-30 02:06:00 5
2015-12-30 02:07:00 10
我正在努力学习线图,并希望在bokeh和matplotlib中完成一个线图。我的第一个选择是bokeh线图示例

我正在使用的matplotlib脚本在下面被注释掉了

我收到了很多很多错误,我会在下面列出这些错误。这些错误的概要让我相信我的datetime格式不正确,我不理解matplotlib中的自动datetime格式。 我还使用正则表达式对日期进行排序,并以正确的格式打印日期

#row[0] is the date
#row[1] is the sales
2015-12-30 02:09:00 1
2015-12-30 02:10:00 3
2015-12-30 02:12:00 2
2015-12-30 02:13:00 1
2015-12-30 02:14:00 18
2015-12-30 02:15:00 1
2015-12-30 02:16:00 10
2015-12-30 02:17:00 2
博克例子

import numpy as np

from bokeh.plotting import figure, output_file, show
import utils
from bokeh.io import output_notebook, show
from bokeh.plotting import figure

# the import from MYSQL works great and data prints 100% works great
db = MySQLdb.connect("all my database stuff")
cur = db.cursor()
cur.execute("SELECT Statement")
#row[0] is the date in the format above "2015-12-30 02:09:00"
#row[1] is the sales in the format above "1"
try:
    for row in cur.fetchall():
    date = row[0]
    sales = row[1]
    #regex to strip date hypens and colons replace with spaces
    #date_clean = re.sub('[^A-Za-z0-9]+', '', date)     
    print date,
    print sales
    #print date_clean prints with no extra characters
except:
    print "error: unable to fetch data"

# prepare some data 
x = 'date'
#x = 'date_clean'
y = 'sales'
# output to static HTML file
output_file("lines.html", title="line plot example")
# create a new plot with a title and axis labels
p = figure(title="example chart", x_axis_label='x', y_axis_label='y')
# add a line renderer with legend and line thickness
p.line(x, y, legend="Temp.", line_width=2)
# show the results
show(p)
当前错误尽管我已经处理了数百个错误。“lines.html”打开时有一个框,但没有行、日期或销售额

找不到记录器的处理程序 “/usr/local/lib/python2.7/dist-packages/bokeh/validation/check.pyc”

(流程:20113):GLib CRITICAL**:g\u slice\u set\u config:assertion “sys\u page\u size==0”失败

下面是我正在使用的matplotlib代码

x = date_clean
y = sales
plt.plot_date(x=date_clean, y=sales)
plt.title("example chart")
plt.ylabel("sales")
plt.grid(True)
plt.show()
"""
有人能给我指一下正确的方向吗。有人能帮我找到日期时间bokeh或matplotlib的正确格式吗
需要执行正确的折线图,并且有人可以解释当前的错误

当您为
x
y
提供标签时,您还应该添加一个源,Bokeh可以根据这些标签从中获取实际数据。目前,您正在告诉Bokeh您的
x
y
数据是字符串,不能绘制为折线图

可能有不同的来源,请参阅:

因此,要么使用:

p.line(x, y, source=...)
或提供实际数据:

p.line(dates, sales)
此外,在创建图形时,将
x轴
指定为日期时间可能会有所帮助,尽管我不认为这是这里的主要问题

p = figure(x_axis_type="datetime", ....

顺便说一句,如果您创建了一个其他人可以运行的示例,这会有所帮助。我没有您的数据库,因此我无法复制您的错误,也无法测试我的(可能的)解决方案。

我曾经/正在遇到类似的问题。我在MPL找到了解决这个问题的方法,但不是Bokeh

对于Matplotlib:

# Format the dates using dates.strftime(format)
# see https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

dates = dates.strftime('%b%d%y') 

# setup figure and axes object
fig = plt.figure(title = 'fig_title_here') #
ax = plt.subplot()

# use regular plot to plot the sales date
ax.plot(range(len(sales)), sales) 
# label x ticks as dates
ax.set_xticklabels([dates])
plt.axis()
希望有帮助。
另外,如果您在bokeh中找到答案,请将其张贴在此处。

我不知道bokeh,但是您在
matplotlib
中得到的错误是什么?对于matplotlib,我得到了QT输出框图1。图中只有一个蓝色的圆点在中间。除标准的后退按钮外,框周围为空。当使用reg表达式时,我得到了对开本错误:错误dt=datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)Overflower错误:使用上述日期-时间格式时,有符号整数大于最大值:错误:返回数组(a,dtype,copy=False,order=order)值错误:float的文字无效():2015-12-30 02:37:00返回数组(a,dtype,copy=False,order=order)value错误:float()的文本无效:2015 12 30 02 37 00