Home>

I am currently making a graph (scatter plot) using Bokeh in Python, but I asked a question because the graph axes and data cannot be combined well.
Below is the image when completed

I want to fix the Y-axis from 00:00:00 to 24:00:00 and match the data with it as shown in this graph. And I want the X axis to be displayed every day even on days without data.

I did a lot of research and wasn't sure, so I'll write a simple program below.

import matplotlib.pyplot as plt
from matplotlib impost dates
xlist = ['2018-08-24', '2018-08-24', '2018-08-25', '2018-08-25', '2018-08-25', '2018-08-23' , '2018-08-23', '2018-08-21', '2018-08-21', '2018-08-19', '2018-08-17', '2018-08-05', ' 2018-07-28 ',' 2018-07-18 ',' 2018-07-18 ',' 2018-07-17 ',' 2018-07-15 ',' 2018-07-11 ',' 2018- 07-10 ',' 2018-07-09 ']
ylist = ['19:46:00', '3:30:00', '3:29:00', '3:26:00', '2:52:00', '14: 36: 00 ' , '2:45:00', '23: 27: 00 ',' 3:56:00 ',' 4:20:00 ',' 2:49:00 ', '22: 47: 00', ' 22:22:00 ', '13: 52: 00', '1:49:00', '17: 48: 00 ', '15: 22: 00', '2:12:00', '18: 27:00 ', '21: 15: 00']
p = figure (plot_width = 400, plot_height = 400, x_axis_type = "datetime")
p.xaxis.axis_label = 'date'
p.yaxis.axis_label = 'time'
p.circle (xlist,
         ylist
         )



It's so white
In the case of matplotlib, which I asked before, the order of the X and Y axes was different, but the data contents were barely displayed. However, in this Bokeh, the X-axis and Y-axis are not displayed and the data is not displayed. I want to be able to scroll horizontally because the amount of data is actually large.
I want to display this on the H-axis: MM: SS on the Y-axis and the day on the X-axis that is not in the data, but please show me how to solve it

Execution environment

windows 10
python 3.6.6
matplotlib 2.2.3
bokeh 0.13.0

  • Answer # 1

    Basically, as withmatplotlib, xlist and ylist are converted to datetime type data instead of character strings, and datetime locators and formatters are set on the X and Y axes. Will

    from bokeh.io import show
    from bokeh.plotting import figure
    from bokeh.models import DatetimeTickFormatter
    from datetime import datetime as dt
    import math
    xlist = ['2018-08-24', '2018-08-24', '2018-08-25', '2018-08-25', '2018-08-25', '2018-08-23' , '2018-08-23', '2018-08-21', '2018-08-21', '2018-08-19', '2018-08-17', '2018-08-05', ' 2018-07-28 ',' 2018-07-18 ',' 2018-07-18 ',' 2018-07-17 ',' 2018-07-15 ',' 2018-07-11 ',' 2018- 07-10 ',' 2018-07-09 ']
    ylist = ['19:46:00', '3:30:00', '3:29:00', '3:26:00', '2:52:00', '14: 36: 00 ' , '2:45:00', '23: 27: 00 ',' 3:56:00 ',' 4:20:00 ',' 2:49:00 ', '22: 47: 00', ' 22:22:00 ', '13: 52: 00', '1:49:00', '17: 48: 00 ', '15: 22: 00', '2:12:00', '18: 27:00 ', '21: 15: 00']
    # convert xlist, ylist to datetime type
    xlist = [dt.strptime (d, '% Y-% m-% d') for d in xlist]
    ylist = [dt.strptime (d, '% H:% M:% S') for d in ylist]
    # Plot data
    p = figure (x_axis_type = 'datetime',
               y_axis_type = 'datetime',
               x_range = (dt.strptime ('2018-7-9', '% Y-% m-% d'),
                        dt.strptime ('2018-8-24', '% Y-% m-% d')),
               y_range = (dt.strptime ('00: 00 ','% H:% M '),
                        dt.strptime ('23: 59 ','% H:% M ')))
    p.circle (xlist, ylist)
    # Set X axis
    x_format = "% m /% d"
    p.xaxis.formatter = DatetimeTickFormatter (
        seconds = [x_format],
        minutes = [x_format],
        hours = [x_format],
        days = [x_format],
        months = [x_format],
        years = [x_format]
    )
    p.xaxis.major_label_orientation = math.radians (90)
    # Set Y-axis
    y_format = "% H:% M"
    p.yaxis.formatter = DatetimeTickFormatter (
        seconds = [y_format],
        minutes = [y_format],
        hours = [y_format],
        days = [y_format],
        months = [y_format],
        years = [y_format]
        )
    show (p)

Related articles