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 (
QPenorNone) – Pen used when drawing axis and (by default) ticks.textPen (
QPenorNone) – Pen used when drawing tick labels.tickPen (
QPenorNone) – Pen used when drawing ticks.linkView (
ViewBoxorNone) – Causes the range of values displayed in the axis to be linked to the visible range of a ViewBox.parent (
QtWidgets.QGraphicsItemorNone) – 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()[source]#
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:
- labelString() str[source]#
Generate the label string based on current label, units, and prefix.
- Returns:
The complete label string, including units and any prefixes.
- Return type:
- 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()[source]#
Return the ViewBox linked to this axis.
- Returns:
The linked ViewBox, or
Noneif there is no ViewBox linked.- Return type:
- logTickStrings( )[source]#
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.
- 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.
- pen() QPen[source]#
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( )[source]#
Set the alpha value for the grid, or
Falseto disable.When grid lines are enabled, the axis tick lines are extended to cover the extent of the linked ViewBox, if any.
- setHeight( )[source]#
Set the height of this axis reserved for ticks and tick labels.
The height of the axis label is automatically added.
- 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 (
tupleoftupleoffloat,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 (
intorfloat, 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( )[source]#
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 (
tupleofbool) – 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 areboolto 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:
See also
setConfigOptionOption to change the default foreground color.
- setRange( )[source]#
Set the range of values displayed by the axis.
Usually this is handled automatically by linking the axis to a ViewBox with
linkToView.- Parameters:
- 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)).
- 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
intThe maximum length of ticks in pixels. Positive values point toward the text; negative values point away.tickTextOffset
intReserved spacing between text and axis in pixels.tickTextWidth
intHorizontal space reserved for tick text in pixels.tickTextHeight
intVertical space reserved for tick text in pixels.autoExpandTextSpace
boolAutomatically expand text space if the tick strings become too long.autoReduceTextSpace
boolAutomatically shrink the axis if necessary.hideOverlappingLabels
boolorintTrue(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.intsets the tolerance limit for how many pixels a label is allowed to extend beyond the axis. Defaults to 15 forhideOverlappingLabels = False.
tickFont
QFontorNoneDetermines the font used for tick values. Use None for the default font.stopAxisAtTick
tuple of
bool, boolThe 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 theAxisItemboundary.
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
boolindicates whether text is displayed adjacent to ticks.tickAlpha
float,intorNoneIfNone, pyqtgraph will draw the ticks with the alpha it deems appropriate. Otherwise, the alpha will be fixed at the value passed. Withint, accepted values are [0..255]. With value of typefloat, accepted values are from [0..1].maxTickLevel
intdefault: 2Tick (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:
- setTextPen(
- *args,
- **kwargs,
Set the pen used for drawing text.
If no arguments given, the default foreground color will be used.
- Parameters:
See also
setConfigOptionOption 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.QFontorNone) – The font to use for the tick values. Set toNonefor 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:
See also
setConfigOptionOption 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:
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( )[source]#
Explicitly determine which ticks to display.
This overrides the behavior specified by
tickSpacing(),tickValues(), andtickStrings().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.See also
tickSpacing()How tick spacing is configured.
tickValues()How tick values are set.
tickStrings()How tick strings are specified.
- setWidth( )[source]#
Set the width of this axis reserved for ticks and tick labels.
The width of the axis label is automatically added.
- 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[source]#
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[source]#
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( )[source]#
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:
- 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, ifresult[0]is(10, 0), then major ticks will be displayed every 10 units and the first major tick will correspond tominVal. If insteadresult[0]is(10, 5), then major ticks will be displayed every 10 units, but the first major tick will correspond tominVal + 5.[ (major_tick_spacing, offset), (minor_tick_spacing, offset), (sub_minor_tick_spacing, offset), ... ]
- Return type:
- tickStrings( )[source]#
Return the strings that should be displayed at each tick value.
This method is used to generate tick strings, and is called automatically.
- tickValues( )[source]#
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:
- 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: