0

When I want to plot a curve f(x) with pyplot, what I usually do is to create a vector X with all the x-values equally spaced:

import numpy as np
X=np.linspace(0.,1.,100) 

then I create the function

def f(x):
    return x**2

and then I make the plot

from matplotlib import pyplot as plt
plt.plot(X,f(X))
plt.show()

However, in some cases I might want the x-values not to be equally spaced, when the function is very stiff in some regions and very smooth in others. What is the correct way to properly choose the best X vector for the function I want to plot?

3sm1r
  • 520
  • 4
  • 19
  • 1
    [This](https://stackoverflow.com/questions/32504766/python-generate-unevenly-spaced-array) might be of help to you – Sheldore Dec 13 '18 at 16:25

1 Answers1

1

In its generality there is not definitive answer to this. But you can of course always choose the complete range with the required density,

X = np.linspace(0.,1., 6000) 

or you can decide for some intervals and set the density differently for those

x1 = np.linspace(0.0,0.5, 60) 
x2 = np.linspace(0.5,0.6, 5000) 
x3 = np.linspace(0.6,1.0, 10) 
X = np.concatenate((x1, x2, x3))
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712