Skip to content

Conversation

@azerupi
Copy link

@azerupi azerupi commented Nov 16, 2025

This PR adds supports for spans in plots on either axis. This allows for example to highlight a region of interest or draw states in the background of plots.

image

There are a few points I would like to highlight that would need some feedback:

  1. The spans are drawn as plot items and therefore they show up in the legend. The structure in the code makes sense but I'm not sure if the spans should be displayed in the legend. With a lot of spans on a plot this could become quite messy. The name of the spans are displayed in the plot themselves already.
  2. The names of the spans are drawn in over them for spans on the x-axis they are drawn at the top-center of the span and for spans on the y-axis they are drawn at the left-middle of the span. When zooming out and the span is too small to display the name it will get truncated and ultimately hidden. I think this looks good and is functional, but this is all very arbitrary so I'm open for other solutions.
  3. The spans being plot items means that they also get drawn in the order they are called in the show closure. This means it could be drawn on top of the line plot. This has advantages and disadvantages. I'm not sure if we want to have spans always be drawn in the background of other plot items?

Copy link
Collaborator

@michalsustr michalsustr left a comment

Choose a reason for hiding this comment

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

@azerupi thanks for the PR, it's very useful!

Feedback:

  1. The spans are drawn as plot items and therefore they show up in the legend. The structure in the code makes sense but I'm not sure if the spans should be displayed in the legend. With a lot of spans on a plot this could become quite messy. The name of the spans are displayed in the plot themselves already.

Agreed. I think it rather makes sense to implement them as PlotItems than the current PlotItemBase.
There is a similar implementation in this fork: 0xb-s#39 (for inspiration) but I think yours is better because of the labels.

  1. The names of the spans are drawn in over them for spans on the x-axis they are drawn at the top-center of the span and for spans on the y-axis they are drawn at the left-middle of the span. When zooming out and the span is too small to display the name it will get truncated and ultimately hidden. I think this looks good and is functional, but this is all very arbitrary so I'm open for other solutions.

I like it! <3

  1. The spans being plot items means that they also get drawn in the order they are called in the show closure. This means it could be drawn on top of the line plot. This has advantages and disadvantages. I'm not sure if we want to have spans always be drawn in the background of other plot items?

I believe spans should be drawn in the background, and other plot items should be overlayed. If it was photoshop layers, I'd make it 0. background (ticks etc), 1. spans 2. plot items (lines, markers, etc.)

Other things that would be great to add:

  • Ability to choose the vertical/horizontal placement of the labels.
  • Semiopen intervals: allow +/- inf spans.

rect_elem::highlighted_color,
};

const LABEL_PADDING: f32 = 4.0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add doc comment, which sides the padding applies to (I think all?)


// If the span is too small to display the full name, find the longest name
// with "..." appended that we can display within the span
fn find_name_candidate(&self, width: f32, painter: &Painter, font_id: &FontId) -> String {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's extract this as a standalone util function, it can be used in other places.

@azerupi
Copy link
Author

azerupi commented Nov 21, 2025

Thank you @michalsustr! This is great feedback and exactly what I was looking for.
Everything you said makes sense to me. I'll try to work on this over the next 10 days.

@michalsustr
Copy link
Collaborator

Great, looking forward!

@michalsustr michalsustr self-assigned this Nov 22, 2025
@michalsustr michalsustr changed the title Add spans for plots feat: Add spans for plots Nov 22, 2025
@michalsustr
Copy link
Collaborator

I was working on the code base over the weekend and got more familiar with it. I changed my mind: I think it should keep being PlotItem as you've done it. What should be done instead is each item should declare if it should be shown in the legend. This is for another independent PR. So please ignore that, and we can finish the PR as it is, without too many changes (just the two little features I outlined). Thank you!

@azerupi
Copy link
Author

azerupi commented Nov 23, 2025

Sounds good!

@azerupi
Copy link
Author

azerupi commented Nov 23, 2025

@michalsustr just to be sure, you still want me to draw the spans on in the background even if we keep them as PlotItem? Is there a way to do this or would this require a new mechanism or workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add vertical and horizontal spans to plots

2 participants