Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AxisItem-based grid item #2579

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

swvanbuuren
Copy link
Contributor

@swvanbuuren swvanbuuren commented Jan 8, 2023

The idea is to display the grid lines in a GridItem and not extend the AxisItems ticks.

This PR attempts to fix #1606, which reports that tick labels never seem to be displayed just right, when the grid is enabled.

Although the current PR seems to accomplish this, I am not quite happy with the current implementation:

  • I had troubles replicating the way how AxisItem calculates tick positions inside a GridItem. Therefore I just added the AxisItem objects to the GridItem and get the positions by calling the appropriate methods. Not nice but it works.
  • With this PR, the GridItem has been improved so that it gets the right tick positions from the corresponding AxisItems. However, it needs to be clarified if this approach shall be adopted altogether by AxisItem, without the need for users to manually create a GridItem.

Therefore, I marked it as draft. I am happy to receive/discuss ideas how the current approach can be improved.

Recently, #2385 was introduced that circumvents this problem, by hiding tick labels, when overlap even slightly the axis boundaries. Thus, the changes of this PR are only relevant if the style option 'hideOverlappingLabels' of all AxisItems are set to False.

I think it is useful, to have the possibility, to show tick labels at the plotted range start and/or end, when the tick label positions exactly match the start and/or end coordinate. Afaik matplotlib also does this.

Example to compare the behavior before and after

import pyqtgraph as pg
pg.setConfigOptions(antialias=True)
from pyqtgraph.Qt import QtWidgets
import numpy as np

win = pg.GraphicsLayoutWidget(show=True)
win.resize(800,350)
win.setWindowTitle('pyqtgraph example: random points')
plt1 = win.addPlot()
plt2 = win.addPlot()

# create data
x = np.arange(100)
y = np.random.normal(size=100)

for ax in ('left', 'right', 'top', 'bottom'):
    plt1.getAxis(ax).setStyle(hideOverlappingLabels=False)
    plt2.getAxis(ax).setStyle(hideOverlappingLabels=False)

plt1.plot(x, y)
plt2.plot(x, y)

plt1.setXLink(plt2)
plt1.setYLink(plt2)

plt1.showAxis('right', show=True)
plt2.showAxis('right', show=True)
plt1.showAxis('top', show=True)
plt2.showAxis('top', show=True)

plt1.showGrid(x=True, y=True)

grid = pg.GridItem()
grid.setTextPen(None)
grid.setTickSpacing(x=[None, None], y=[None, None])
plt1.addItem(grid)

plt2.showGrid(x=True, y=True)

QtWidgets.QApplication.instance().exec_()

pyqtgraph/graphicsItems/GridItem.py Fixed Show resolved Hide resolved
- Adopt @j9ac9k's proposals

Signed-off-by: Sietze van Buuren <s.van.buuren@gmail.com>
@j9ac9k
Copy link
Member

j9ac9k commented Jul 10, 2023

Hi @swvanbuuren

Sorry I've let this PR sit for so long, I'll try and review/test here in the coming days.

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.

showGrid screws up tick labels at axis boundaries
2 participants