[education/labplot] lib/examples: [scripting] simplified the code in a couple of example projects by using horizontalAxis() and verticalAxis() from CartesianPlot.

Alexander Semke <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 8a2c1e10e1529996394211b197028b0b99f89b11 by Alexander Semke.
Committed on 09/08/2026 at 08:24.
Pushed by asemke into branch 'master'.

[scripting] simplified the code in a couple of example projects by using horizontalAxis() and verticalAxis() from CartesianPlot.

M  +24   -26   lib/examples/Data = Smooth + Rough/script.py
M  +2    -5    lib/examples/ENSO/script.py
M  +2    -6    lib/examples/Helium Rydberg Spectrum/script.py
M  +8    -21   lib/examples/Logarithmic Scales/script.py
M  +2    -5    lib/examples/Quantum Wave Packet/script.py
M  +49   -52   lib/examples/Space Debris/script.py

https://invent.kde.org/education/labplot/-/commit/8a2c1e10e1529996394211b197028b0b99f89b11

diff --git a/lib/examples/Data = Smooth + Rough/script.py b/lib/examples/Data = Smooth + Rough/script.py
index d6f24c2a74..fb9dfd87d2 100644
--- a/lib/examples/Data = Smooth + Rough/script.py	
+++ b/lib/examples/Data = Smooth + Rough/script.py	
@@ -67,19 +67,18 @@ rangeY1.setRange(300, 650)
 plotArea1.setRange(CartesianCoordinateSystem.Dimension.Y, 0, rangeY1)
 plotArea1.enableAutoScale(CartesianCoordinateSystem.Dimension.Y, 0, False)
 
-for axis in plotArea1.children(AspectType.Axis):
-    if axis.orientation() == WorksheetElement.Orientation.Horizontal and axis.position() == Axis.Position.Bottom:
-        te10.setText("index")
-        axis.title().setText(te10.toHtml())
-        axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
-        axis.minorGridLine().setStyle(Qt.PenStyle.DashLine)
-        axis.setLabelsFont(fo7)
-    elif axis.orientation() == WorksheetElement.Orientation.Vertical and axis.position() == Axis.Position.Left:
-        te10.setText("data")
-        axis.title().setText(te10.toHtml())
-        axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
-        axis.minorGridLine().setStyle(Qt.PenStyle.DashLine)
-        axis.setLabelsFont(fo7)
+x_axis = plotArea1.horizontalAxis()
+te10.setText("index")
+x_axis.title().setText(te10.toHtml())
+x_axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
+x_axis.minorGridLine().setStyle(Qt.PenStyle.DashLine)
+x_axis.setLabelsFont(fo7)
+y_axis = plotArea1.verticalAxis()
+te10.setText("data")
+y_axis.title().setText(te10.toHtml())
+y_axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
+y_axis.minorGridLine().setStyle(Qt.PenStyle.DashLine)
+y_axis.setLabelsFont(fo7)
 
 worksheet.addChild(plotArea1)
 
@@ -153,19 +152,18 @@ rangeY2.setRange(-120, 100)
 plotArea2.setRange(CartesianCoordinateSystem.Dimension.Y, 0, rangeY2)
 plotArea2.enableAutoScale(CartesianCoordinateSystem.Dimension.Y, 0, False)
 
-for axis in plotArea2.children(AspectType.Axis):
-    if axis.orientation() == WorksheetElement.Orientation.Horizontal and axis.position() == Axis.Position.Bottom:
-        te10.setText("index")
-        axis.title().setText(te10.toHtml())
-        axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
-        axis.minorGridLine().setStyle(Qt.PenStyle.DashLine)
-        axis.setLabelsFont(fo7)
-    elif axis.orientation() == WorksheetElement.Orientation.Vertical and axis.position() == Axis.Position.Left:
-        te10.setText("rough")
-        axis.title().setText(te10.toHtml())
-        axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
-        axis.minorGridLine().setStyle(Qt.PenStyle.DashLine)
-        axis.setLabelsFont(fo7)
+x_axis = plotArea2.horizontalAxis()
+te10.setText("index")
+x_axis.title().setText(te10.toHtml())
+x_axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
+x_axis.minorGridLine().setStyle(Qt.PenStyle.DashLine)
+x_axis.setLabelsFont(fo7)
+y_axis = plotArea2.verticalAxis()
+te10.setText("rough")
+y_axis.title().setText(te10.toHtml())
+y_axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
+y_axis.minorGridLine().setStyle(Qt.PenStyle.DashLine)
+y_axis.setLabelsFont(fo7)
 
 worksheet.addChild(plotArea2)
 
diff --git a/lib/examples/ENSO/script.py b/lib/examples/ENSO/script.py
index 7a0de2fc34..8f8a6fa13a 100644
--- a/lib/examples/ENSO/script.py
+++ b/lib/examples/ENSO/script.py
@@ -47,11 +47,8 @@ border = CartesianPlot.BorderTypeFlags.BorderLeft | CartesianPlot.BorderTypeFlag
 plotArea.setBorderType(border)
 worksheet.addChild(plotArea)
 
-for axis in plotArea.children(AspectType.Axis):
-    if axis.orientation() == WorksheetElement.Orientation.Horizontal and axis.position() == Axis.Position.Bottom:
-        axis.title().setText("Month")
-    elif axis.orientation() == WorksheetElement.Orientation.Vertical and axis.position() == Axis.Position.Left:
-        axis.title().setText("Atmospheric Pressure")
+plotArea.horizontalAxis().title().setText("Month")
+plotArea.verticalAxis().title().setText("Atmospheric Pressure")
 
 data = XYCurve("data")
 data.setPlotType(Plot.PlotType.Scatter)
diff --git a/lib/examples/Helium Rydberg Spectrum/script.py b/lib/examples/Helium Rydberg Spectrum/script.py
index 78b144c684..6ccd89bdb9 100644
--- a/lib/examples/Helium Rydberg Spectrum/script.py	
+++ b/lib/examples/Helium Rydberg Spectrum/script.py	
@@ -57,12 +57,8 @@ plotArea.setBottomPadding(Worksheet.convertToSceneUnits(1.5, Worksheet.Unit.Cent
 
 worksheet.addChild(plotArea)
 
-for axis in plotArea.children(AspectType.Axis):
-    if axis.orientation() == WorksheetElement.Orientation.Horizontal and axis.position() == Axis.Position.Bottom:
-        axis.title().setText("wavelength (nm)")
-    elif axis.orientation() == WorksheetElement.Orientation.Vertical and axis.position() == Axis.Position.Left:
-        axis.title().setText("Count")
-
+plotArea.horizontalAxis().title().setText("wavelength (nm)")
+plotArea.verticalAxis().title().setText("Count")
 config1 = XYCurve("config1")
 plotArea.addChild(config1)
 config1.setPlotType(Plot.PlotType.Line)
diff --git a/lib/examples/Logarithmic Scales/script.py b/lib/examples/Logarithmic Scales/script.py
index fb65d87114..d849a267c4 100644
--- a/lib/examples/Logarithmic Scales/script.py	
+++ b/lib/examples/Logarithmic Scales/script.py	
@@ -45,11 +45,8 @@ plotArea.title().setPosition(QPointF(Worksheet.convertToSceneUnits(0, Worksheet.
 
 worksheet.addChild(plotArea)
 
-for axis in plotArea.children(AspectType.Axis):
-    if axis.orientation() == WorksheetElement.Orientation.Horizontal and axis.position() == Axis.Position.Bottom:
-        axis.title().setText("x")
-    elif axis.orientation() == WorksheetElement.Orientation.Vertical and axis.position() == Axis.Position.Left:
-        axis.title().setText("f(x)")
+plotArea.horizontalAxis().title().setText("x")
+plotArea.verticalAxis().title().setText("f(x)")
 
 config11 = XYEquationCurve("f(x)=10^x")
 plotArea.addChild(config11)
@@ -115,11 +112,8 @@ plotArea1.title().setPosition(QPointF(Worksheet.convertToSceneUnits(0, Worksheet
 
 worksheet.addChild(plotArea1)
 
-for axis in plotArea1.children(AspectType.Axis):
-    if axis.orientation() == WorksheetElement.Orientation.Horizontal and axis.position() == Axis.Position.Bottom:
-        axis.title().setText("x")
-    elif axis.orientation() == WorksheetElement.Orientation.Vertical and axis.position() == Axis.Position.Left:
-        axis.title().setText("f(x)")
+plotArea1.horizontalAxis().title().setText("x")
+plotArea1.verticalAxis().title().setText("f(x)")
 
 config21 = XYEquationCurve("f(x)=10^x")
 plotArea1.addChild(config21)
@@ -186,11 +180,8 @@ plotArea2.title().setPosition(QPointF(Worksheet.convertToSceneUnits(0, Worksheet
 
 worksheet.addChild(plotArea2)
 
-for axis in plotArea2.children(AspectType.Axis):
-    if axis.orientation() == WorksheetElement.Orientation.Horizontal and axis.position() == Axis.Position.Bottom:
-        axis.title().setText("x")
-    elif axis.orientation() == WorksheetElement.Orientation.Vertical and axis.position() == Axis.Position.Left:
-        axis.title().setText("f(x)")
+plotArea2.horizontalAxis().title().setText("x")
+plotArea2.verticalAxis().title().setText("f(x)")
 
 config31 = XYEquationCurve("f(x)=10^x")
 plotArea2.addChild(config31)
@@ -257,12 +248,8 @@ plotArea3.title().setPosition(QPointF(Worksheet.convertToSceneUnits(0, Worksheet
 
 worksheet.addChild(plotArea3)
 
-for axis in plotArea3.children(AspectType.Axis):
-    axis.setMinorTicksLength(0)
-    if axis.orientation() == WorksheetElement.Orientation.Horizontal and axis.position() == Axis.Position.Bottom:
-        axis.title().setText("x")
-    elif axis.orientation() == WorksheetElement.Orientation.Vertical and axis.position() == Axis.Position.Left:
-        axis.title().setText("f(x)")
+plotArea3.horizontalAxis().title().setText("x")
+plotArea3.verticalAxis().title().setText("f(x)")
 
 config41 = XYEquationCurve("f(x)=10^x")
 plotArea3.addChild(config41)
diff --git a/lib/examples/Quantum Wave Packet/script.py b/lib/examples/Quantum Wave Packet/script.py
index f28e0d44b5..080a7ffa43 100644
--- a/lib/examples/Quantum Wave Packet/script.py	
+++ b/lib/examples/Quantum Wave Packet/script.py	
@@ -132,11 +132,8 @@ if create_objects:
 	plotArea.title().setText("Quantum Wave Packet Evolution")
 
 	# Configure axes
-	for axis in plotArea.children(AspectType.Axis):
-		if axis.orientation() == WorksheetElement.Orientation.Horizontal and axis.position() == Axis.Position.Bottom:
-			axis.title().setText("Position x")
-		elif axis.orientation() == WorksheetElement.Orientation.Vertical and axis.position() == Axis.Position.Left:
-			axis.title().setText("ψ(x,t)")
+	plotArea.horizontalAxis().title().setText("Position x")
+	plotArea.verticalAxis().title().setText("ψ(x,t)")
 
 	worksheet.addChild(plotArea)
 
diff --git a/lib/examples/Space Debris/script.py b/lib/examples/Space Debris/script.py
index e880a143a0..49c3912baf 100644
--- a/lib/examples/Space Debris/script.py	
+++ b/lib/examples/Space Debris/script.py	
@@ -97,23 +97,22 @@ rangeY1.setRange(0, 10)
 plotArea1.setRange(CartesianCoordinateSystem.Dimension.Y, 0, rangeY1)
 plotArea1.enableAutoScale(CartesianCoordinateSystem.Dimension.Y, 0, False)
 
-for axis in plotArea1.children(AspectType.Axis):
-    if axis.orientation() == WorksheetElement.Orientation.Horizontal and axis.position() == Axis.Position.Bottom:
-        te.clear()
-        te.setFontPointSize(8)
-        te.append("Year")
-        axis.title().setText(te.toHtml())
-        axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
-        axis.minorGridLine().setStyle(Qt.PenStyle.DotLine)
-        axis.setLabelsFont(fo6)
-    elif axis.orientation() == WorksheetElement.Orientation.Vertical and axis.position() == Axis.Position.Left:
-        te.clear()
-        te.setFontPointSize(8)
-        te.append("breakups")
-        axis.title().setText(te.toHtml())
-        axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
-        axis.minorGridLine().setStyle(Qt.PenStyle.DotLine)
-        axis.setLabelsFont(fo6)
+x_axis = plotArea1.horizontalAxis()
+te.clear()
+te.setFontPointSize(8)
+te.append("Year")
+x_axis.title().setText(te.toHtml())
+x_axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
+x_axis.minorGridLine().setStyle(Qt.PenStyle.DotLine)
+x_axis.setLabelsFont(fo6)
+y_axis = plotArea1.verticalAxis()
+te.clear()
+te.setFontPointSize(8)
+te.append("breakups")
+y_axis.title().setText(te.toHtml())
+y_axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
+y_axis.minorGridLine().setStyle(Qt.PenStyle.DotLine)
+y_axis.setLabelsFont(fo6)
 
 worksheet.addChild(plotArea1)
 
@@ -164,23 +163,22 @@ rangeY2.setRange(0, 7)
 plotArea2.setRange(CartesianCoordinateSystem.Dimension.Y, 0, rangeY2)
 plotArea2.enableAutoScale(CartesianCoordinateSystem.Dimension.Y, 0, False)
 
-for axis in plotArea2.children(AspectType.Axis):
-    if axis.orientation() == WorksheetElement.Orientation.Horizontal and axis.position() == Axis.Position.Bottom:
-        te.clear()
-        te.setFontPointSize(8)
-        te.append("Altitude [km]")
-        axis.title().setText(te.toHtml())
-        axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
-        axis.minorGridLine().setStyle(Qt.PenStyle.DotLine)
-        axis.setLabelsFont(fo6)
-    elif axis.orientation() == WorksheetElement.Orientation.Vertical and axis.position() == Axis.Position.Left:
-        te.clear()
-        te.setFontPointSize(8)
-        te.append("density [10-8km-3]")
-        axis.title().setText(te.toHtml())
-        axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
-        axis.minorGridLine().setStyle(Qt.PenStyle.DotLine)
-        axis.setLabelsFont(fo6)
+x_axis = plotArea2.horizontalAxis()
+te.clear()
+te.setFontPointSize(8)
+te.append("Altitude [km]")
+x_axis.title().setText(te.toHtml())
+x_axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
+x_axis.minorGridLine().setStyle(Qt.PenStyle.DotLine)
+x_axis.setLabelsFont(fo6)
+y_axis = plotArea2.verticalAxis()
+te.clear()
+te.setFontPointSize(8)
+te.append("density [10-8km-3]")
+y_axis.title().setText(te.toHtml())
+y_axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
+y_axis.minorGridLine().setStyle(Qt.PenStyle.DotLine)
+y_axis.setLabelsFont(fo6)
 
 worksheet.addChild(plotArea2)
 
@@ -249,24 +247,23 @@ rangeY3.setRange(1, 10000)
 plotArea3.setRange(CartesianCoordinateSystem.Dimension.Y, 0, rangeY3)
 plotArea3.enableAutoScale(CartesianCoordinateSystem.Dimension.Y, 0, False)
 
-for axis in plotArea3.children(AspectType.Axis):
-    if axis.orientation() == WorksheetElement.Orientation.Horizontal and axis.position() == Axis.Position.Bottom:
-        te.clear()
-        te.setFontPointSize(8)
-        te.append("Altitude [km]")
-        axis.title().setText(te.toHtml())
-        axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
-        axis.minorGridLine().setStyle(Qt.PenStyle.DotLine)
-        axis.setLabelsFont(fo6)
-        axis.setScalingFactor(1000)
-    elif axis.orientation() == WorksheetElement.Orientation.Vertical and axis.position() == Axis.Position.Left:
-        te.clear()
-        te.setFontPointSize(8)
-        te.append("density [10-12km-3]")
-        axis.title().setText(te.toHtml())
-        axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
-        axis.minorGridLine().setStyle(Qt.PenStyle.DotLine)
-        axis.setLabelsFont(fo6)
+x_axis = plotArea3.horizontalAxis()
+te.clear()
+te.setFontPointSize(8)
+te.append("Altitude [km]")
+x_axis.title().setText(te.toHtml())
+x_axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
+x_axis.minorGridLine().setStyle(Qt.PenStyle.DotLine)
+x_axis.setLabelsFont(fo6)
+x_axis.setScalingFactor(1000)
+y_axis = plotArea3.verticalAxis()
+te.clear()
+te.setFontPointSize(8)
+te.append("density [10-12km-3]")
+y_axis.title().setText(te.toHtml())
+y_axis.majorGridLine().setStyle(Qt.PenStyle.SolidLine)
+y_axis.minorGridLine().setStyle(Qt.PenStyle.DotLine)
+y_axis.setLabelsFont(fo6)
 
 worksheet.addChild(plotArea3)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.