Skip to main content
`Ctrl`+`K`

# AxisItem

class pyqtgraph.AxisItem(

  orientation: str,
  pen=None,
  textPen=None,
  tickPen=None,
  linkView=None,
  parent=None,
  maxTickLength=-5,
  showValues=True,
  **args,

)

  GraphicsItem showing a single plot axis with ticks, values, and label.

  Can be configured to fit on any side of a plot, automatically synchronize its displayed scale with ViewBox items. Ticks can be extended to draw a grid.

  If maxTickLength is negative, ticks point into the plot.

  Parameters:

    - orientation (`{'left', 'right', 'top', 'bottom'}`) – The side of the plot the axis is attached to.

    - pen (`QPen` or `None`) – Pen used when drawing axis and (by default) ticks.

    - textPen (`QPen` or `None`) – Pen used when drawing tick labels.

    - tickPen (`QPen` or `None`) – Pen used when drawing ticks.

    - linkView (`ViewBox` or `None`) – Causes the range of values displayed in the axis to be linked to the visible range of a ViewBox.

    - parent (`QtWidgets.QGraphicsItem` or `None`) – Parent Qt object to set to. End users are not expected to set, pyqtgraph should set correctly on its own.

    - maxTickLength (`int`) – Maximum length of ticks to draw in pixels. Negative values draw into the plot, positive values draw outward. Default -5.

    - showValues (`bool`) – Whether to display values adjacent to ticks. Default true.

    - **args – All additional keyword arguments are passed to `setLabel()`.

  __init__(
    orientation: str,
    pen=None,
    textPen=None,
    tickPen=None,
    linkView=None,
    parent=None,
    maxTickLength=-5,
    showValues=True,
    **args,
  )
  enableAutoSIPrefix(
    enable=True,
  )

    Enable (or disable) automatic SI prefix scaling on this axis.

    When enabled, this feature automatically determines the best SI prefix to prepend to the label units, while ensuring that axis values are scaled accordingly.

    For example, if the axis spans values from -0.1 to 0.1 and has units set to ‘V’ then the axis would display values -100 to 100 and the units would appear as ‘mV’

    This feature is enabled by default, and is only available when a suffix (unit string) is provided to display on the label.

    Parameters:

      enable (`bool`, optional) – Enable Auto SI prefix, by default True.

  getSIPrefixEnableRanges()

    Get the ranges in which automatic SI prefix scaling is enabled.

    Returns:

      A tuple of ranges where SI prefix scaling is enabled. Each range is a tuple containing two floats representing the start and end of the range. If no custom ranges are set, then the default ranges are returned. The default ranges are `((0., 1.), (1e9, inf))` if units are empty, and `((0., inf))` otherwise.

    Return type:

      `tuple` of `tuple` of `float`, `float`

  labelString() → str

    Generate the label string based on current label, units, and prefix.

    Returns:

      The complete label string, including units and any prefixes.

    Return type:

      `str`

  linkToView(
    view,
  )

    Link to a ViewBox, causing its displayed range to match the view range.

    This is usually called automatically by the ViewBox.

    Parameters:

      view (`ViewBox`) – The view to link to.

  linkedView()

    Return the ViewBox linked to this axis.

    Returns:

      The linked ViewBox, or `None` if there is no ViewBox linked.

    Return type:

      `ViewBox`

  linkedViewChanged(
    view,
    newRange=None,
  )

    Call when the linked view range has changed.

    Parameters:

      - view (`ViewBox`) – The view whose range has changed.

      - newRange (`tuple` of `float`, `float`, optional) – The new range of the view, by default None.

  logTickStrings(
    values: list[float],
    scale: float,
    spacing: float,
  )

    Return the strings that should be displayed at each tick value in log mode.

    This method is called by `tickStrings()` when the axis is in logarithmic mode. It is a good method to override in subclasses.

    Parameters:

      - values (`list` of `float`) – List of tick values.

      - scale (`float`) – The scaling factor for tick values.

      - spacing (`float`) – The spacing between ticks.

    Returns:

      List of strings to display at each tick value.

    Return type:

      `list` of `str`

  logTickValues(
    minVal,
    maxVal,
    size,
    stdTicks,
  )

    Return tick values for log-scale axes.

    This method is called by `tickValues()` when the axis is in logarithmic mode. It is a good method to override in subclasses.

    Parameters:

      - minVal (`float`) – Minimum value to generate tick values for.

      - maxVal (`float`) – Maximum value to generate tick values for.

      - size (`float`) – The length of the axis in pixels.

      - stdTicks (`list` of `tuple` of `float`, `float`) – The tick values generated by the standard `tickValues()` method.

    Returns:

      A list of tuples, one for each tick level. Each tuple contains two values: `(spacing, values)`, where spacing is the distance between ticks and values is a list of tick values.

    Return type:

      `list` of `tuple` of `float`, `float` or `list` of `tuple` of `None`, `float`

  pen() → QPen

    Get the pen used for drawing text, axes, ticks, and grid lines.

    If no custom pen has been set, this method will return a pen with the default foreground color.

    Returns:

      The pen used to draw text, axes, ticks, and grid lines.

    Return type:

      `QPen`

  setGrid(
    grid: int | float | bool,
  )

    Set the alpha value for the grid, or `False` to disable.

    When grid lines are enabled, the axis tick lines are extended to cover the extent of the linked ViewBox, if any.

    Parameters:

      grid (`bool` or `int` or `float`) – Alpha value to apply to `GridItem`.

      - `False` - Disable the grid.

      - `int` - Values between [0, 255] to set the alpha of the grid to.

      - `float` - Values between [0..1] to set the alpha of the grid to.

  setHeight(
    h: int | None = None,
  )

    Set the height of this axis reserved for ticks and tick labels.

    The height of the axis label is automatically added.

    Parameters:

      h (`int` or `None`, optional) – If `None`, then the value will be determined automatically based on the size of the tick text, by default None.

  setLabel(
    text: str | None = None,
    units: str | None = None,
    unitPrefix: str | None = None,
    siPrefixEnableRanges: tuple[tuple[float, float], ...] | None = None,
    unitPower: int | float = 1,
    **kwargs,
  )

    Set the text displayed adjacent to the axis.

    Parameters:

      - text (`str`) – The text (excluding units) to display on the label for this axis.

      - units (`str`) – The units for this axis. Units should generally be given without any scaling prefix (eg, ‘V’ instead of ‘mV’). The scaling prefix will be automatically prepended based on the range of data displayed.

      - unitPrefix (`str`) – An extra prefix to prepend to the units.

      - siPrefixEnableRanges (`tuple` of `tuple` of `float`, `float`, `Optional`) – The ranges in which automatic SI prefix scaling is enabled. Defaults to everywhere, unless units is empty, in which case it defaults to `((0., 1.), (1e9, inf))`.

      - unitPower (`int` or `float`, optional) – The power to which the units are raised. For example, if units=’m²’, the unitPower should be 2. This ensures correct scaling when using SI prefixes. Supports positive, negative and non-integral powers. Default is 1. Note: The power only affects the scaling, not the units themselves. For example, with units=’m’ and unitPower=2, the displayed units will still be ‘m’.

      - **kwargs – All extra keyword arguments become CSS style options for the `<span>` tag which will surround the axis label and units. Note that CSS attributes are not always valid python arguments. Examples: `color='#FFF'`, `**{'font-size': '14pt'}`.

    Notes

    The final text generated for the label will usually take the form:

        <span style="...args...">{text} (prefix{units})</span>

  setLogMode(
    *args: tuple[bool] | tuple[bool, bool] | None,
    **kwargs: dict[str, bool] | None,
  )

    Set log scaling for x and / or y axes.

    If two positional arguments are provided, the first will set log scaling for the x axis and the second for the y axis. If a single positional argument is provided, it will set the log scaling along the direction of the AxisItem. Alternatively, x and y can be passed as keyword arguments.

    If an axis is set to log scale, ticks are displayed on a logarithmic scale and values are adjusted accordingly. The linked ViewBox will be informed of the change.

    Parameters:

      - *args (`tuple` of `bool`) – If length 1, sets log mode regardless of orientation. If length 2, the first element toggles log mode for x-axis, and the second element toggles log mode for the y-axis.

      - **kwargs (`dict`) – Pass a dictionary with keys x and y, where the values are `bool` to set the log mode for the respective x or y axis. Trying to set the y axis log mode while this axis item is horizontal (or vice versa) will be ignored.

    See also

    `setLogMode()`

      The method called to shift the values of the data.

  setPen(
    *args,
    **kwargs,
  )

    Set the pen used for drawing text, axes, ticks, and grid lines.

    If no arguments given, the default foreground color will be used.

    Parameters:

      - *args (`tuple`) – Arguments relayed to `mkPen()`.

      - **kwargs (`dict`) – Arguments relayed to :func:`~pyqtgraph.mkPen.

    See also

    `setConfigOption`

      Option to change the default foreground color.

  setRange(
    mn: float,
    mx: float,
  )

    Set the range of values displayed by the axis.

    Usually this is handled automatically by linking the axis to a ViewBox with `linkToView`.

    Parameters:

      - mn (`float`) – Bottom value to set the range to.

      - mx (`float`) – Top value to set the range to.

    Raises:

      ValueError – When non-finite values are passed.

  setSIPrefixEnableRanges(
    ranges=None,
  )

    Set the ranges in which automatic SI prefix scaling is enabled.

    This function allows you to define specific ranges where SI prefixes will be used. By default, SI prefix scaling is enabled everywhere, unless units are empty, in which case it defaults to `((0., 1.), (1e9, inf))`.

    Parameters:

      ranges (`tuple` of `tuple` of `float`, `float`, optional) – A tuple of ranges where SI prefix scaling is enabled. Each range is a tuple containing two floats representing the start and end of the range.

  setScale(
    scale=1.0,
  )

    Set the value scaling for this axis.

    Setting this value causes the axis to draw ticks and tick labels as if the view coordinate system were scaled.

    Parameters:

      scale (`float`, optional) – Value to scale the drawing of ticks and tick labels as if the view coordinate system was scaled, by default 1.0.

  setStyle(
    **kwargs,
  )

    Set various style options.

    Parameters:

      **kwargs (`dict`, optional) – Here are a list of supported arguments.

      Property | Description
      --- | ---
      tickLength | `int` The maximum length of ticks in pixels. Positive values point toward the text; negative values point away.
      tickTextOffset | `int` Reserved spacing between text and axis in pixels.
      tickTextWidth | `int` Horizontal space reserved for tick text in pixels.
      tickTextHeight | `int` Vertical space reserved for tick text in pixels.
      autoExpandTextSpace | `bool` Automatically expand text space if the tick strings become too long.
      autoReduceTextSpace | `bool` Automatically shrink the axis if necessary.

      hideOverlappingLabels

      `bool` or `int`

      - `True` (default for horizontal axis): Hide tick labels which extend beyond the AxisItem’s geometry rectangle.

      - `False` (default for vertical axis): Labels may be drawn extending beyond the extent of the axis.

      - `int` sets the tolerance limit for how many pixels a label is allowed to extend beyond the axis. Defaults to 15 for `hideOverlappingLabels = False`.

      tickFont | `QFont` or `None` Determines the font used for tick values. Use None for the default font.

      stopAxisAtTick

      tuple of `bool, bool` The first element represents the horizontal axis, the second element represents the vertical axis.

      - `True` - The axis line is drawn only as far as the last tick.

      - `False` - The line is drawn to the edge of the `AxisItem` boundary.

      textFillLimits

      list of `(int, float)` This structure determines how the AxisItem decides how many ticks should have text appear next to them. The first value corresponds to the tick number. The second value corresponds to the fill percentage. Each tuple in the list specifies what fraction of the axis length may be occupied by text, given the number of ticks that already have text displayed.

      For example

          [
              # Never fill more than 80% of the axis
              (0, 0.8),
              # If we already have 2 ticks with text, fill no
              # more than 60% of the axis
              (2, 0.6),
              # If we already have 4 ticks with text, fill no
              # more than 40% of the axis
              (4, 0.4),
              # If we already have 6 ticks with text, fill no
              # more than 20% of the axis
              (6, 0.2)
          ]

      showValues | `bool` indicates whether text is displayed adjacent to ticks.
      tickAlpha | `float`, `int` or `None` If `None`, pyqtgraph will draw the ticks with the alpha it deems appropriate. Otherwise, the alpha will be fixed at the value passed. With `int`, accepted values are [0..255]. With value of type `float`, accepted values are from [0..1].

      maxTickLevel

      `int` default: 2

      Tick (and grid line) density level.

      - 0: Show major ticks only

      - 1: Show major ticks and one level of minor ticks

      - 2: Show major ticks and two levels of minor ticks (higher CPU usage)

    Raises:

      - NameError – Raised when the name of a keyword argument is not recognized.

      - TypeError – Raised when a value for a keyword argument is of the wrong type.

  setTextPen(
    *args,
    **kwargs,
  )

    Set the pen used for drawing text.

    If no arguments given, the default foreground color will be used.

    Parameters:

      - *args (`tuple`) – Arguments relayed to `mkPen()`.

      - **kwargs (`dict`) – Arguments relayed to :func:`~pyqtgraph.mkPen.

    See also

    `setConfigOption`

      Option to change the default foreground color.

  setTickDensity(
    density=1.0,
  )

    Set the density of ticks displayed on the axis.

    A higher density value means that more ticks will be displayed. The density value is used in conjunction with the tickSpacing method to determine the actual tick locations.

    Parameters:

      density (`float`, optional) – Density of ticks to display, by default 1.0.

  setTickFont(
    font: QFont | None,
  )

    Set the font used for tick values.

    Parameters:

      font (`QtGui.QFont` or `None`) – The font to use for the tick values. Set to `None` for the default font.

  setTickPen(
    *args,
    **kwargs,
  )

    Set the pen used for drawing ticks.

    If no arguments given, the default foreground color will be used.

    Parameters:

      - *args (`tuple`) – Arguments relayed to `mkPen()`.

      - **kwargs (`dict`) – Arguments relayed to :func:`~pyqtgraph.mkPen.

    See also

    `setConfigOption`

      Option to change the default foreground color.

  setTickSpacing(
    major: float | None = None,
    minor: float | None = None,
    levels: list[tuple[float, float]] | None = None,
  )

    Explicitly determine the spacing of major and minor ticks.

    This overrides the default behavior of the tickSpacing method, and disables the effect of setTicks(). Arguments may be either major and minor, or levels which is a list of `(spacing, offset)` tuples for each tick level desired. If no arguments are given, then the default behavior of tickSpacing is enabled.

    Parameters:

      - major (`float`, optional) – Spacing for major ticks, by default None.

      - minor (`float`, optional) – Spacing for minor ticks, by default None.

      - levels (`list` of `tuple` of `float`, `float`, optional) – A list of (spacing, offset) tuples for each tick level, by default None.

    Examples

        # two levels, all offsets = 0
        axis.setTickSpacing(5., 1.)
        # three levels, all offsets = 0
        axis.setTickSpacing(levels=[(3., 0.), (1., 0.), (0.25, 0.)])
        # reset to default
        axis.setTickSpacing()

  setTicks(
    ticks: list[list[tuple[float, str]]] | None,
  )

    Explicitly determine which ticks to display.

    This overrides the behavior specified by `tickSpacing()`, `tickValues()`, and `tickStrings()`.

    The format for ticks looks like:

        [
            [
                (majorTickValue1, majorTickString1),
                (majorTickValue2, majorTickString2),
                ...
            ],
            [
                (minorTickValue1, minorTickString1),
                (minorTickValue2, minorTickString2),
                ...
            ],
            ...
        ]

    The two levels of major and minor ticks are expected. A third tier of additional ticks is optional. If ticks is `None`, then the default tick system will be used.

    Parameters:

      ticks (`list` of `list` of `float`, `str` or `None`) – Explicitly set tick display information.

    See also

    `tickSpacing()`

      How tick spacing is configured.

    `tickValues()`

      How tick values are set.

    `tickStrings()`

      How tick strings are specified.

  setWidth(
    w: int | None = None,
  )

    Set the width of this axis reserved for ticks and tick labels.

    The width of the axis label is automatically added.

    Parameters:

      w (`int` or `None`, optional) – If `None`, then the value will be determined automatically based on the size of the tick text, by default None.

  showLabel(
    show: bool = True,
  )

    Show or hide the label text for this axis.

    Parameters:

      show (`bool`, optional) – Show the label text, by default True.

  textPen() → QPen

    Get the pen used for drawing text.

    If no custom text pen has been set, this method will return a pen with the default foreground color.

    Returns:

      The pen used to draw text.

    Return type:

      `QPen`

  tickPen() → QPen

    Get the pen used for drawing ticks.

    If no custom tick pen has been set, this method will return the axis’s main pen.

    Returns:

      The pen used to draw tick marks.

    Return type:

      `QPen`

  tickSpacing(
    minVal: float,
    maxVal: float,
    size: float,
  )

    Determine the spacing of ticks on the axis.

    This method is called whenever the axis needs to be redrawn and is a good method to override in subclasses that require control over tick locations.

    Parameters:

      - minVal (`float`) – Minimum value being displayed on the axis.

      - maxVal (`float`) – Maximum value being displayed on the axis.

      - size (`float`) – Length of the axis in pixels.

    Returns:

      A list of tuples, one for each tick level. Each tuple contains two values: `(spacing, offset)`. The spacing value is the distance between ticks, and the offset is the first tick relative to minVal. For example, if `result[0]` is `(10, 0)`, then major ticks will be displayed every 10 units and the first major tick will correspond to `minVal`. If instead `result[0]` is `(10, 5)`, then major ticks will be displayed every 10 units, but the first major tick will correspond to `minVal + 5`.

          [
              (major_tick_spacing, offset),
              (minor_tick_spacing, offset),
              (sub_minor_tick_spacing, offset),
              ...
          ]

    Return type:

      `list` of `tuple` of `float`, `float`

  tickStrings(
    values: list[float],
    scale: float,
    spacing: float,
  )

    Return the strings that should be displayed at each tick value.

    This method is used to generate tick strings, and is called automatically.

    Parameters:

      - values (`list` of `float`) – List of tick values.

      - scale (`float`) – The scaling factor for tick values.

      - spacing (`float`) – The spacing between ticks.

    Returns:

      List of strings to display at each tick value.

    Return type:

      `list` of `str`

  tickValues(
    minVal: float,
    maxVal: float,
    size: float,
  )

    Return the values and spacing of ticks to draw.

    The values returned are essentially the same as those returned by `tickSpacing()`, but with the addition of explicit tick values for each tick level. This method is a good method to override in subclasses.

    Parameters:

      - minVal (`float`) – Minimum value to generate tick values for.

      - maxVal (`float`) – Maximum value to generate tick values for.

      - size (`float`) – The length of the axis in pixels.

    Returns:

      A list of tuples, one for each tick level. Each tuple contains two values: `(spacing, values)`, where spacing is the distance between ticks and values is a list of tick values.

    Return type:

      `list` of `tuple` of `float`, `list` of `float`

  unlinkFromView()

    Unlink this axis from its linked ViewBox.