Skip to content

Add ability to specify color and font of axis tick labels for each axis#155

Merged
emilk merged 8 commits into
emilk:mainfrom
TommiKabelitz:feat/tick-label-decoration
Mar 26, 2026
Merged

Add ability to specify color and font of axis tick labels for each axis#155
emilk merged 8 commits into
emilk:mainfrom
TommiKabelitz:feat/tick-label-decoration

Conversation

@TommiKabelitz

@TommiKabelitz TommiKabelitz commented Nov 19, 2025

Copy link
Copy Markdown
Contributor

This allows specifying the color and font (through FontId) of the tick labels for each axis on a plot
image

All feedback and criticism of the API and implementation are much appreciated. I will note, to ensure that the fading of the tick label color is respected, I did the following

let text_color = if let Some(color) = self.hints.tick_label_color {
    color.gamma_multiply(strength.sqrt())
} else {
    super::color_from_strength(ui, strength)
};

and I don't love the duplication of the gamma_multiply, I just didn't want to touch the color_from_strength function without the all clear.

Comment thread egui_plot/src/axis.rs Outdated
pub(super) placement: Placement,
pub(super) label_spacing: Rangef,
pub(super) tick_label_color: Option<egui::Color32>,
pub(super) tick_label_font: Option<egui::FontId>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import egui::FontId directly

Comment thread egui_plot/src/axis.rs Outdated
pub(super) min_thickness: f32,
pub(super) placement: Placement,
pub(super) label_spacing: Rangef,
pub(super) tick_label_color: Option<egui::Color32>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import egui::Color32 directly

Comment thread egui_plot/src/axis.rs
Axis::X => Rangef::new(60.0, 80.0), // labels can get pretty wide
Axis::Y => Rangef::new(20.0, 30.0), // text isn't very high
},
tick_label_color: None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the default?
maybe set it to a default which we already use

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else use Default::default()

@TommiKabelitz TommiKabelitz Nov 24, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the default is whatever is in ui.visuals().text_color() when the plot is actually rendered because this function

/// Determine a color from a 0-1 strength value.
pub fn color_from_strength(ui: &Ui, strength: f32) -> Color32 {
    let base_color = ui.visuals().text_color();
    base_color.gamma_multiply(strength.sqrt())
}

is called to determine the color when the plot is shown.

I am still just calling this function when color is None so the behavior is not changing. I could potentially move the default to the instantiation of AxisHints, but that would change the behavior in cases where ui.visuals().text_color() is changed between creation of the axes and showing the plot.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works for me

Comment thread egui_plot/src/axis.rs Outdated

/// Set the font of the axis tick labels.
///
/// To change the color of the tick labels see [`Self::tick_label_color`].

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is not necessary for this func

Comment thread egui_plot/src/axis.rs Outdated
/// As labels get close, they will fade in color until they become invisible. See
/// [`Self::label_spacing`].
///
/// To change the font of the tick labels see [`Self::tick_label_font`].

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is not necessary for this func

Comment thread egui_plot/src/axis.rs Outdated
let label_font_id = self
.hints
.tick_label_font
.clone()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2x clones?
Is there something else possible?

@TommiKabelitz TommiKabelitz Nov 24, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be mistaken, but I don't think so. Cloning FontId is pretty cheap as it is just an f32 and the FontFamily enum which is an Arc.

I think you need to clone in both cases as layout_no_wrap expects an owned FontId. When the font has not been specified we use the global one so you need to clone. When it is specified, you have to clone as we only have a reference to self and so cannot move it out of the shared reference.

The implications of changing layout_no_wrap to accept an owned value look to be significant at a glance.

@TommiKabelitz TommiKabelitz requested a review from bircni November 26, 2025 00:51
bircni

This comment was marked as resolved.

Comment thread egui_plot/src/axis.rs Outdated
@emilk emilk added enhancement New feature or request include in changelog This change will be included in the changelog labels Mar 26, 2026
@emilk emilk merged commit fdcebd5 into emilk:main Mar 26, 2026
10 checks passed
AlecTietjens pushed a commit to AlecTietjens/egui_plot that referenced this pull request Apr 11, 2026
…is (emilk#155)

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui_plot/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo f or a new
example.
* Do NOT open PR:s from your `master` or `main` branch, as that makes it
hard for maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! We will review your PR, but our time is limited!
-->

This allows specifying the color and font (through FontId) of the tick
labels for each axis on a plot
<img width="1329" height="867" alt="image"
src="https://github.com/user-attachments/assets/a5e683e4-0163-40d4-8955-0ca106ec12d0"
/>

All feedback and criticism of the API and implementation are much
appreciated. I will note, to ensure that the fading of the tick label
color is respected, I did the following

```
let text_color = if let Some(color) = self.hints.tick_label_color {
    color.gamma_multiply(strength.sqrt())
} else {
    super::color_from_strength(ui, strength)
};
```
and I don't love the duplication of the gamma_multiply, I just didn't
want to touch the color_from_strength function without the all clear.


* Closes <emilk#154>
* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Tommi Kabelitz <tommi.kabelitz@priorianalytica.com>
Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request include in changelog This change will be included in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ability to specify the color of the axis tick labels

3 participants