3

I am attempting to show a tick for every single one of my datapoints in a timeseries line chart, where the timeseries can be resampled to a different rate depending on a user's choice.

Minute - Hour - Day - Month.

I would like for each tick to be an hour if the user chooses hour, a day if the user chooses day and so on and so forth.

I would then like these ticks to stay fixed so that bokeh does not create new sub-hour, sub-day ticks inbetween when I zoom the chart.

I haved tried using desired_num_ticks on the x-axis, but as I zoom the chart I believe that the number of ticks in the chart remains the same and therefore gives me ticks below the timescale that I have specified.

First I resample the data based off of mean or sum, depending on the timescale.

   # Read ##########################################################################
    # Resample to Hour by average
    if resample_rate == 'T':
        pass
    else:
        df = df.resample('H').mean()
        if resample_rate == 'H':
            pass
        else:
            df = df.resample('{}'.format(resample_rate)).sum()

Then I attempt to plot my data. Timeseries formatting works correctly, but I have not been able to get the x-axis to behave as I want.

    energy_plot = figure(plot_width=1400,
                         plot_height=600,
                         title="Timeseries DC Power",
                         x_axis_type='datetime')

    # Label Axes
    energy_plot.xaxis[0].axis_label = 'Months'
    energy_plot.yaxis[0].axis_label = 'DC Power [W]'

    # background color
    energy_plot.background_fill_color = "white"
    energy_plot.background_fill_alpha = 0.5

    # grid lines
    energy_plot.xgrid.grid_line_color = None
    energy_plot.ygrid.grid_line_color = None

    colors = itertools.cycle(Dark2_5)

    # plot all dc power
    for column, label, c in zip(string_db_list, labels, colors):
        energy_plot.line(df.index,
                     df['{}'.format(column)],
                     line_width=4,
                     alpha=0.8,
                     color=c,
                     visible=False,
                     legend="{}".format(label))


    # clicking on the legend hides selection
    energy_plot.legend.click_policy = "hide"

    # Format the xaxis as datetime ticks
    try:
        energy_plot.xaxis.formatter = DatetimeTickFormatter(
            hours=["%d %B %Y"],
            days=["%d %B %Y"],
            months=["%d %B %Y"],
            years=["%d %B %Y"],)
        energy_plot.xaxis.major_label_orientation = 3.14 / 4
        energy_plot.xaxis[0].num_minor_ticks = 0
        energy_plot.xaxis[0].desired_num_ticks = df.index
    except:
        pass

When the chart initializes, the number of ticks looks good, though it doesn't remain as I zoom in.

day level resample

skrhee
  • 336
  • 5
  • 19
  • Why is the is formatting in a `try...except block`? This looks more like a categorical axis (i.e. displaying date *strings*, not real datetime values). It's hard to say more without a complete minimal reproducer. – bigreddot Jul 30 '19 at 18:50
  • have you tried manually defining the ticker for the x axes ? https://docs.bokeh.org/en/latest/docs/reference/models/axes.html#bokeh.models.axes.Axis.ticker – Pablo Nov 18 '19 at 18:40

0 Answers0