3sm1r
3sm1r

Reputation: 530

non equally spaced points along x-axis in a plot

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?

Upvotes: 0

Views: 700

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339340

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))

Upvotes: 1

Related Questions