Skip to content

Add style option 'hideOverlappingLabels'#2385

Merged
j9ac9k merged 2 commits intopyqtgraph:masterfrom
bbc131:axis_labels_overlapping
Sep 2, 2022
Merged

Add style option 'hideOverlappingLabels'#2385
j9ac9k merged 2 commits intopyqtgraph:masterfrom
bbc131:axis_labels_overlapping

Conversation

@bbc131
Copy link
Copy Markdown
Contributor

@bbc131 bbc131 commented Aug 4, 2022

  • With 'hideOverlappingLabels==False' nothing changes.
    Tick labels might be drawn overlapping with tick labels from
    neighboring plots or overlap with other items or might be simply
    clipped.
  • With 'hideOverlappingLabels==True', only tick labels, which are
    fully contained within the items geometry rectangle are drawn.

Fixes the following problem

The tick labels of AxisItem can be drawn outside of GraphicsWidget.geometry() rectangle, therefore, they might overlap with neighboring items, e.g. tick labels from other plots or simply be clipped.

It's very easy to reproduce this with the GraphicsLayout example, just zoom and pan the view, to provoke some labels with long values at the axis edges. See also the following screenshot with overlapping / clipped tick labels.

overlapping_axis_labels

In comparison to that, the screenshot below shows the result of this fix which means the labels which would overlap or be clipped haven't been drawn.

overlapping_axis_labels_afterwards

* With 'hideOverlappingLabels==False' nothing changes.
  Tick labels might be drawn overlapping with tick labels from
  neighboring plots or overlap with other items or might be simply
  clipped.
* With 'hideOverlappingLabels==True', only tick labels, which are
  fully contained within the items geometry rectangle are drawn.
@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Aug 5, 2022

Hi @bbc131 thanks for the PR!

Code diff looks fine, I just left one in-line comment. Read the docs doesn't like the changes to the docstring, here is the blurb that it is emitting warnings/errors about.

/home/docs/checkouts/readthedocs.org/user_builds/pyqtgraph/checkouts/2385/doc/source/../../pyqtgraph/graphicsItems/AxisItem.py:docstring of pyqtgraph.graphicsItems.AxisItem.AxisItem.setStyle:6: ERROR: Unexpected indentation.
/home/docs/checkouts/readthedocs.org/user_builds/pyqtgraph/checkouts/2385/doc/source/../../pyqtgraph/graphicsItems/AxisItem.py:docstring of pyqtgraph.graphicsItems.AxisItem.AxisItem.setStyle:8: WARNING: Block quote ends without a blank line; unexpected unindent.
/home/docs/checkouts/readthedocs.org/user_builds/pyqtgraph/checkouts/2385/doc/source/../../pyqtgraph/graphicsItems/AxisItem.py:docstring of pyqtgraph.graphicsItems.AxisItem.AxisItem.setStyle:12: ERROR: Unexpected indentation.
/home/docs/checkouts/readthedocs.org/user_builds/pyqtgraph/checkouts/2385/doc/source/../../pyqtgraph/graphicsItems/AxisItem.py:docstring of pyqtgraph.graphicsItems.AxisItem.AxisItem.setStyle:13: WARNING: Block quote ends without a blank line; unexpected unindent.
/home/docs/checkouts/readthedocs.org/user_builds/pyqtgraph/checkouts/2385/doc/source/../../pyqtgraph/graphicsItems/AxisItem.py:docstring of pyqtgraph.graphicsItems.AxisItem.AxisItem.setStyle:15: ERROR: Unexpected indentation.
/home/docs/checkouts/readthedocs.org/user_builds/pyqtgraph/checkouts/2385/doc/source/../../pyqtgraph/graphicsItems/AxisItem.py:docstring of pyqtgraph.graphicsItems.AxisItem.AxisItem.setStyle:17: WARNING: Block quote ends without a blank line; unexpected unindent.
/home/docs/checkouts/readthedocs.org/user_builds/pyqtgraph/checkouts/2385/doc/source/../../pyqtgraph/graphicsItems/AxisItem.py:docstring of pyqtgraph.graphicsItems.AxisItem.AxisItem.setStyle:45: WARNING: Definition list ends without a blank line; unexpected unindent.
/home/docs/checkouts/readthedocs.org/user_builds/pyqtgraph/checkouts/2385/doc/source/../../pyqtgraph/graphicsItems/AxisItem.py:docstring of pyqtgraph.graphicsItems.AxisItem.AxisItem.setStyle:45: CRITICAL: Unexpected section title or transition.

Copy link
Copy Markdown
Member

@ixjlyons ixjlyons left a comment

Choose a reason for hiding this comment

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

This looks like a nice improvement to me.

I wonder if it makes sense to expose the allowable overlap value itself. I notice matplotlib gives axis tick labels a little extra space outside the axis line, but that could be because of some padding at an Axes or Figure level or something, I'm not sure. I struggle to think of a good name for such a parameter though, which probably means it's better as on/off.

I also don't know about changing behavior to default 0. It seems like a more reasonable default than 15 though I'd say.

Set various style options.

=================== =======================================================
===========================================================================
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Need to preserve the breaks in the top and bottom table borders, lined up with the column of space between the argument names and descriptions.

@bbc131 bbc131 force-pushed the axis_labels_overlapping branch from 1ab274a to 76111d5 Compare August 22, 2022 11:50
@bbc131
Copy link
Copy Markdown
Contributor Author

bbc131 commented Aug 22, 2022

I wonder if it makes sense to expose the allowable overlap value itself. I notice matplotlib gives axis tick labels a little extra space outside the axis line, but that could be because of some padding at an Axes or Figure level or something, I'm not sure. I struggle to think of a good name for such a parameter though, which probably means it's better as on/off.

I think it makes sense to have this value being equal to the surrounding layout's border margins. So if you use different margins, you may want to change this value.

#p.drawText(rect, textFlags, vstr)

br = self.boundingRect()
if not br.contains(rect):
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 just realized, that my note in the commit message

With 'hideOverlappingLabels==False' nothing changes.

isn't true. Originally, the labels get clipped, if they go over the bounding rect. After my change, they will be hidden, independent of hideOverlappingLabels.
So, to get really the same behaviour as originally with hideOverlappingLabels==False, one should change the following.

Suggested change
if not br.contains(rect):
if self.style['hideOverlappingLabels'] and not br.contains(rect):

But, as painting outside of the bounding rect is not allowed (see https://doc.qt.io/qt-5/qgraphicsitem.html#paint), this clipping should additionally be implemented or we simply keep the hiding enabled if it goes out ot the bounding rect.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given that painting outside boundingRect I'm good w/ how it currently is

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Sep 1, 2022

I wonder if it makes sense to expose the allowable overlap value itself. I notice matplotlib gives axis tick labels a little extra space outside the axis line, but that could be because of some padding at an Axes or Figure level or something, I'm not sure. I struggle to think of a good name for such a parameter though, which probably means it's better as on/off.

I think it makes sense to have this value being equal to the surrounding layout's border margins. So if you use different margins, you may want to change this value.

that's fairly sensible, would you be up for changing the 0 default value to self.layout.getContentsMargins.

@bbc131
Copy link
Copy Markdown
Contributor Author

bbc131 commented Sep 1, 2022

I wonder if it makes sense to expose the allowable overlap value itself. I notice matplotlib gives axis tick labels a little extra space outside the axis line, but that could be because of some padding at an Axes or Figure level or something, I'm not sure. I struggle to think of a good name for such a parameter though, which probably means it's better as on/off.

I think it makes sense to have this value being equal to the surrounding layout's border margins. So if you use different margins, you may want to change this value.

that's fairly sensible, would you be up for changing the 0 default value to self.layout.getContentsMargins.

I quickly looked into this using the GraphicsLayout example and it seems to be not so straight forward as I thought:

For example the bottom AxisItem of "Plot 1" is embedded in a layout handled by the PlotItem with default margins of 1. The visible margins are added by the superior GraphicsLayout, named l in the example.

So, first problem: It's not easy to decide from which layout the margins should be taken.

Further, this aforementioned superior layout l also contains a label on the left side (text: "Long vertical label"). And also on the right side of "Plot 1" is another plot located. So, obviously, using the contentsMargins is not really what we need if there are neighboring elements in the layout, one rather should consider the spacing between the elements in this case.

So all in all I would say, that it would be too much to consider all these possibilities. I suggest to stick with a constant default value.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Sep 2, 2022

Thanks for investigating @bbc131 this LGTM, merging.

@j9ac9k j9ac9k merged commit 18e1721 into pyqtgraph:master Sep 2, 2022
ntjess pushed a commit to ntjess/pyqtgraph that referenced this pull request Sep 12, 2022
* Add style option 'hideOverlappingLabels'

* With 'hideOverlappingLabels==False' nothing changes.
  Tick labels might be drawn overlapping with tick labels from
  neighboring plots or overlap with other items or might be simply
  clipped.
* With 'hideOverlappingLabels==True', only tick labels, which are
  fully contained within the items geometry rectangle are drawn.

* Fix docstring
swvanbuuren added a commit to swvanbuuren/pyqtgraph that referenced this pull request Oct 21, 2022
Signed-off-by: Sietze van Buuren <s.van.buuren@gmail.com>
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.

3 participants