2016-01-02 36 views
1

我在構建我的第一個線圖時遇到了一些主要問題,目前我一直在努力工作和研究我的錯誤,很長一段時間都在耗盡所有努力。如何爲Python格式化日期時間線圖散景/ matplotlib日期時間和圖形問題

我有以下格式的銷售日期時間示例數據。該數據是從我的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 

我上線圖努力工作,想完成在這兩個背景虛化和matplotlib線圖。我的第一個選擇是散景線圖示例here

我正在使用的matplotlib腳本在下面註釋。

我收到了許多許多錯誤,並會在下面列出錯誤。錯誤的總體概要導致我相信我的日期時間格式不正確,我不明白在matplotlib中格式化的自動日期時間。 我也一直使用正則表達式對日期進行排序並以正確的格式打印日期。

#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」打開了一個框,但沒有行,日期或銷售。

No handlers could be found for logger "/usr/local/lib/python2.7/dist-packages/bokeh/validation/check.pyc"

(process:20113): GLib-CRITICAL **: g_slice_set_config: assertion 'sys_page_size == 0' failed

這裏是我使用

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() 
""" 

有人點我在正確的方向上matplotlib代碼。任何人都可以幫助使用正確格式的日期時間散景或matplotlib 要求執行一個正確的線圖,並有人可以解釋當前的錯誤。

+0

我不知道「散景」,但是你用'matplotlib'得到的錯誤是什麼? –

+0

for matplotlib我得到了QT輸出框圖1.圖中只有一個藍色圓點。除了標準的後退按鈕之外,框周圍是空的。使用reg表達式時出現對角線錯誤 – jedimonk

+0

:error dt = datetime.datetime.fromordinal(ix).replace(tzinfo = UTC) OverflowError:有符號整數大於最大值 – jedimonk

回答

3

當您爲xy提供標籤時,還應根據這些標籤添加Bokeh可以從中獲取實際數據的來源。目前,您正在告訴Bokeh,您的xy數據是字符串,不能繪製爲折線圖。

不同來源是可能的,參見: http://bokeh.pydata.org/en/0.10.0/docs/reference/models/sources.html

因此,無論使用:

p.line(x, y, source=...) 

或提供的實際數據:

p.line(dates, sales) 

此外,創建該圖的情況下,指定x axis是一個日期時間可能會幫助,但我不認爲這是主要問題在這裏。

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

順便說一句,它可以幫助你創建一個別人可以運行的例子。我沒有你的數據庫,所以我不能複製你的錯誤,或者測試我的(可能的)解決方案。

0

我有/有類似的問題。我在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() 

希望有所幫助。 另外,如果您在散景中找到答案,請在此處發佈。