Skip to content

Creating a line plot with x_axis_type='log' fails when x_max < 1 #5389

@StevenCHowell

Description

@StevenCHowell

When creating a plot with a logarithmic scale on the x-axis, if the maximum x-value is less than 1 the x-axis decreases from left to right, the scale is linear, not logarithmic, and the default view does not show the data. I tested various values for the maximum x-value and found that when x_max > 1, the x-axis increases left to right but the default view only shows x > 1. This code demonstrates the problem

import numpy as np
from bokeh.plotting import figure, output_file, show

output_file('test.html')

x = np.linspace(0, 0.95) # x decreases left to right, linear scale
# x = np.linspace(0, 1) # x increases left to right but default view shows, 1 < x < 1.05
# x = np.linspace(0, 10) # x increases left to right but default view shows, 1 < x < 10.5
y = np.sin(x)/x

p = figure(x_axis_type='log')
p.line(x, y)

show(p)

I tried declaring the x_range for the figure using x_range=(x.min(), x.max()) but this had to effect unless I removed the zero point from the data, i.e., x = x[x > 0]. With theses changes, the x-axis increases left to right and the default view shows the range indicated. Here is a functional workaround

import numpy as np
from bokeh.plotting import figure, output_file, show

output_file('test.html')

x = np.linspace(0, 0.95) # x decreases left to right, linear scale
x = x[x > 0]
y = np.sin(x)/x

p = figure(x_axis_type='log', x_range=(x.min(), x.max()))
p.line(x, y)

show(p)

I did not test all these same conditions with y_axis_type but I expect it to have the same problems. I did notice that the default view only shows y > 1, even if the data begins ay y < 1.

Here are the key points that need to be verified for logarithmic x and y axes:

  • make sure the x-axis decreases left to right when the maximum x-value is less than 1
  • make sure the y-axis increases bottom to top when the maximum y-value is less than 1
  • adjust the default view to ignore x<=0 or y<=0 but show all other data (including x<1 and y<1)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions