i was create a simple export code using pyqgis3 and atlas print composer map layout on QGIS3.

my question is about map scale.

if i use this line :

mapScale = map.scale()

i take the scale from map and i can use this number on grid interval as i want.

but after generate atlas mode and using this settings for atlas scale


then the map scale change.

i want to take with some way to take the scale from the atlas features after generate or how to use expression on define IntervalX like this .

expression123 = "$scale*10"
qgs_layout_item_map_grid = QgsLayoutItemMapGrid("map 1", map)

i try to use this but without success.


1 Answer 1


The part where you were directly stucked should be related to the following

newGrid = QgsLayoutItemMapGrid ('My new grid', referenceMap)

You can find below a long correction if you want to use existing grid (if already in your layout) or by adding a new grid

project = QgsProject.instance()
manager = project.layoutManager()
layout = manager.layoutByName('mygridname')
referenceMap = layout.referenceMap()

# We supposed you set a grid manually here
firstGrid = referenceMap.grid()
# You can also use referenceMap.grids().asList() if more than one grid

# Else, you can add the grid with
newGrid = QgsLayoutItemMapGrid ('My new grid', referenceMap)

# INFO: We continue with `firstGrid` variable but you could
# replace all the following code by using `newGrid` instead

# To get the non dynamic properties for intervalX & intervalY

# To activate the grid

# Your expression as a string
expressionScale = '@map_scale / 100'
# To get the dynamic properties for intervalX e.g QgsLayoutObject.MapGridIntervalX
# & intervalY e.g QgsLayoutObject.MapGridIntervalY
dataDefinedMapGridIntervalX = firstGrid.dataDefinedProperties().property(QgsLayoutObject.MapGridIntervalX)
dataDefinedMapGridIntervalY = firstGrid.dataDefinedProperties().property(QgsLayoutObject.MapGridIntervalY)

# Set the expression

# Activate both properties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.