1

I am having some major issues building my first ever line graph and currently I have been working and researching my errors for a long time exhausting all efforts.

I have the following date time example data with sales in the following format. The data is being imported from my MySQL database

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

I am working hard on line graphs and would like to complete a line graph in both bokeh and matplotlib. My first choice is the bokeh line graph example here.

The matplotlib scripts I am using are commented out below.

I have received many, many errors and will list the errors below as I go. The general synopsis of the errors leads me to believe my datetime is not formatted correctly and I do not understand the Auto datetime formatted in matplotlib. I have also been using regular expressions to sort the date and print the date in the correct format.

#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

bokeh example

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)

Current errors although I have worked through hundreds of errors. The "lines.html" opens up with a box but no line, dates or sales.

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

here is the matplotlib code I am using

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

Can someone point me in the correct directions. Can anyone help with the correct format for the date time bokeh or matplotlib requires to execute a correct line graph and can someone explain the current error.

5
  • I don't know bokeh, but what is the error you get with matplotlib? Commented Jan 2, 2016 at 2:08
  • for matplotlib I get the QT output box figure 1. Inside the graph is only a single blue dot in the middle. around the box is empty except for the standard back forward buttons. I get the folioing errors Commented Jan 2, 2016 at 2:28
  • when using reg expressions: error dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC) OverflowError: signed integer is greater than maximum Commented Jan 2, 2016 at 2:29
  • When using the date time format above: Error: return array(a, dtype, copy=False, order=order) ValueError: invalid literal for float(): 2015-12-30 02:37:00 Commented Jan 2, 2016 at 2:31
  • return array(a, dtype, copy=False, order=order) ValueError: invalid literal for float(): 2015 12 30 02 37 00 Commented Jan 2, 2016 at 2:58

2 Answers 2

4

When you provide labels for the x and y, you should also add a source from which Bokeh can get the actual data, based on those labels. Currently you are telling Bokeh that your x and y data are strings, which cant be plotted as a line chart.

Different sources are possible, see:

https://docs.bokeh.org/en/latest/docs/reference/models/sources.html

So either use:

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

or provide the actual data:

p.line(dates, sales)

Additionally, when creating the figure, specifying the x axis to be a datetime might help, although i dont think thats the main issue here.

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

Btw, it helps if you create an example that others can run. I don't have your database, so i cant replicate your error, or test my (possible) solution.

Sign up to request clarification or add additional context in comments.

Comments

0

I have had/ am having similar issues. I've found a way around this in MPL but not Bokeh.

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

Hope that helps. Also if you find the answer in bokeh, please post it here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.