Grid Lines - QwtPlotGrid¶
QwtPlotGrid is a plot item for drawing coordinate grids on the plot canvas. Grid lines help users read data point coordinates more accurately and are a common auxiliary element in scientific charts.
Key Features¶
Features
- Major/minor grid lines: Supports both major and minor tick grid lines
- Independent X/Y axis control: Enables or disables horizontal and vertical grids separately
- Style customization: Major and minor grids can have different colors, line widths, and line styles
- Auto-follow ticks: Grid line positions automatically follow axis tick divisions
Basic Concepts¶
Grid Line Types¶
QwtPlotGrid supports four types of grid lines:
| Type | Description |
|---|---|
| X-axis major grid | Vertical lines at X-axis major tick positions |
| X-axis minor grid | Vertical lines at X-axis minor tick positions |
| Y-axis major grid | Horizontal lines at Y-axis major tick positions |
| Y-axis minor grid | Horizontal lines at Y-axis minor tick positions |
Grid Structure Diagram¶
1 2 3 4 5 6 | |
Class Relationships¶
classDiagram
class QwtPlotItem {
+attach(plot)
+detach()
+setZ(z)
+rtti()
}
class QwtPlotGrid {
+enableX(bool)
+enableY(bool)
+enableXMin(bool)
+enableYMin(bool)
+setMajorPen(pen)
+setMinorPen(pen)
+setXDiv(scaleDiv)
+setYDiv(scaleDiv)
}
QwtPlotItem <|-- QwtPlotGrid
Usage¶
1. Basic Usage¶
Create and add a grid to the plot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Grid Layer Order
Grids should typically be drawn below other plot items. By default, QwtPlotGrid has a low Z value and is drawn first. Use grid->setZ(-10) or similar methods to adjust if needed.
2. Grid Line Style Configuration¶
Setting Major Grid Style¶
1 2 3 4 5 6 7 8 9 | |
Setting Minor Grid Style¶
Minor grid lines are typically thinner or dashed compared to major grid lines:
1 2 3 4 5 6 | |
Setting All Grid Lines Uniformly¶
1 2 | |
3. Enabling/Disabling Specific Grid Lines¶
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
4. Custom Tick Divisions¶
Grid lines follow axis tick divisions by default. You can also manually specify ticks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
Tick Division Types
QwtScaleDiv supports three tick levels:
- MajorTick - Major ticks (numeric labels displayed)
- MinorTick - Minor ticks (small tick marks)
- MediumTick - Medium ticks (between major and minor)
Complete Example¶
The following example demonstrates complete grid configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
Core Methods Summary¶
| Method | Description |
|---|---|
enableX(bool) |
Enable/disable X-axis major grid |
enableY(bool) |
Enable/disable Y-axis major grid |
enableXMin(bool) |
Enable/disable X-axis minor grid |
enableYMin(bool) |
Enable/disable Y-axis minor grid |
xEnabled() |
Check X-axis major grid state |
yEnabled() |
Check Y-axis major grid state |
xMinEnabled() |
Check X-axis minor grid state |
yMinEnabled() |
Check Y-axis minor grid state |
setMajorPen() |
Set major grid pen |
setMinorPen() |
Set minor grid pen |
majorPen() |
Get major grid pen |
minorPen() |
Get minor grid pen |
setPen() |
Set all grid pens uniformly |
setXDiv() |
Set X-axis tick division |
setYDiv() |
Set Y-axis tick division |
xScaleDiv() |
Get X-axis tick division |
yScaleDiv() |
Get Y-axis tick division |
Special Meaning of Zero Line Width
In Qt, a line width of 0 means using a "cosmetic line width" (a fast 1-pixel-wide rendering line). For grid lines, using 0.0 as the line width produces the thinnest possible lines.
Related Examples
- All plotting examples include grids:
examples/2D/directory