bircastri
bircastri

Reputation: 2169

Bokeh chart set x axis label

I m building a charts page using bokeh to plot them. I have some Line chart where I need to plot on x-axis the number of week of the year.

So this is the result that I m able to build: enter image description here

As you can see there are some poin on X axis with 09/2023 and 10/2023 at last point.

It is possible to display only the point where are the values, like this? enter image description here

This is the code I used to build this chart:

plotIndicatori = figure(plot_width=int(850), plot_height=int(165),
                               tools="pan,box_zoom,reset",
                               title=nomeIndicatore,
                            x_axis_type="datetime")
            plotIndicatori.toolbar.logo = None
            plotIndicatori.y_range.start = 0
            plotIndicatori.y_range.end = rangeMax + 5
            plotIndicatori.line(x='DateTime', y='Valore', color="navy", alpha=1, source=sourceIndicatori)
            plotIndicatori.circle(x='DateTime', y='Valore', color="navy", alpha=1, source=sourceIndicatori)
            # plotSfera.axis.visible = False
            plotIndicatori.xgrid.grid_line_color = None
            plotIndicatori.outline_line_color = background
            plotIndicatori.border_fill_color = background
            plotIndicatori.background_fill_color = background

Upvotes: 0

Views: 532

Answers (1)

mosc9575
mosc9575

Reputation: 6347

The DatetimeTickFormatter from bokeh offers you multiple options to format the date string on your x-axis. The options %U, %V and %Wall offer the number of weeks. You have to select the one that works best for you. Read the docs to get the complete list of available options.

Minimal Example

import pandas as pd
df = pd.DataFrame(
    {'data':list(range(367))},
    index=pd.date_range('2020-01-01', '2021-01-01', freq='D')
)

from bokeh.models import DatetimeTickFormatter
from bokeh.plotting import show, figure, output_notebook
output_notebook()

p = figure(width=300, height=300, x_axis_type='datetime')
p.xaxis[0].formatter = DatetimeTickFormatter(days="%U", months="%m %U")
p.line(x='index', y='data', source=df)
show(p)

Output

applied datetimetickformatter

Upvotes: 0

Related Questions