How to create a 2D mesh with refinement? Python

How can I create a mesh with refinement, lets say x and y go from 0 to 1 and between 0.4 and 0.6 in both directions I want the points to be closer.

I have been reading about numpy.logspace, but it doesnt allow much freedom.

Any ideas?

Upvotes: 1

Views: 1369

Answers (2)

Peter
Peter

Reputation: 13475

It depends on how you want to "squash" the middle values. But say you want to do it quadratically, using an 11-by-11 grid spanning from -10 to 10 on each axis:

a = np.linspace(-np.sqrt(10), np.sqrt(10), 11)
b = np.sign(a)*a**2
x, y = np.meshgrid(b, b)

To see what this grid looks like:

import matplotlib.pyplot as plt
plt.plot(x.flatten(), y.flatten(), '*')
plt.show()

Upvotes: 0

chthonicdaemon
chthonicdaemon

Reputation: 19750

There isn't one specific command with this functionality, but you can build arrays up with numpy.concatenate this way:

import numpy

start = 0
end = 1
bigstep = 0.1

refinedstart = 0.4
refinedend = 0.6
smallstep = 0.01

x = numpy.concatenate([numpy.arange(start, refinedstart, bigstep),
                       numpy.arange(refinedstart, refinedend, smallstep),
                       numpy.arange(refinedend, end, bigstep)])

Upvotes: 1

Related Questions