And that's what I'm talking about: is there a way to calculate (not empirically guess) the values for SetMargin depending on the nx and ny values?
"Empyrically guessing" is writing code like this
if nx > 10 and nx < 20:
margin_x = 0.5
else if nx > 20 and nx < 50:
margin_y = 0.027