Luc_B
Luc_B

Reputation: 11

Python : Color scatter by datetimes column with Bokeh

With Bokeh, I'm trying to color scatter with datetime values and create a colorbar with a datetime scale.

(something like this) :

Origin pro Graph

A sample of the timeseries :

    Date                Rate    Level
    01/01/2019 08:59    38.3    -19.7
    02/01/2019 09:04    39.1    -21
    01/01/2019 09:09    40.7    -31
    01/01/2019 09:14    42.1    -15
    01/01/2019 09:19    43.6    -14
    01/01/2019 09:24    46.8    -19.7

I tried to plot Rate=f(Level) :

cwd=os.getcwd()
delimit_file=','
fichier = 'qsv1.csv'

qsv = pd.read_csv(fichier,delimiter=delimit_file, encoding = 'ISO-8859-1')
qsv['Date'] =  pd.to_datetime(qsv['Date'], format='%Y/%m/%d %H:%M')


p = figure(x_axis_type="datetime", plot_width=800, plot_height=500,)

source = ColumnDataSource(qsv)


exp_cmap = LinearColorMapper(palette="Viridis256", 
                             low = min(qsv["Date"]), 
                             high = max(qsv["Date"]))

p.circle("Rate", "Level", source=source, line_color=None,
        fill_color={"field":"Date", "transform":exp_cmap})
#p.line("Date", "QS_conv", source=source, color='navy', legend='moyenne glissante')

bar = ColorBar(color_mapper=exp_cmap, location=(0,0))
p.add_layout(bar, "left")

show(p)

But i get :

ValueError: expected a value of type Real, got 2019-01-01 00:04:00 of type Timestamp

Someone knows how to solve this problem?

Upvotes: 0

Views: 423

Answers (2)

Luc_B
Luc_B

Reputation: 11

Thanks, it works by converting datetime in ms

qsv['actualDateTime'] = qsv['Date'].astype(np.int64) / int(1e6)

exp_cmap1 = LinearColorMapper(palette="Viridis256", 
                             low = min(qsv['actualDateTime']), 
                             high = max(qsv['actualDateTime']))

p1.circle("Debit, "Niveau", source=source, line_color=None,
        fill_color={"field":"actualDateTime", "transform":exp_cmap1})

bar1 = ColorBar(color_mapper=exp_cmap1, location=(0,0), formatter=DatetimeTickFormatter(days=["%d/%m/%y"]), label_standoff=12)

Upvotes: 1

bigreddot
bigreddot

Reputation: 34568

Please always include a full stack trace, not just one line. Presumably, this message is from setting, e.g.

low = min(qsv["Date"])

The configuration properties of the linear color mapper do not expect anything other than plain numbers. The underlying units of datetimes in Bokeh are Milliseconds since Epoch so that is what you should convert your datetime values to before passing to the color mapper.

Upvotes: 0

Related Questions