7
$\begingroup$

I need help plotting the Heaviside function:

Real analysis often involves constructing bizarre functions which are intuitively correct, but ultimately wrong. See the great book Counterexamples in Analysis.


I wrote a numpy script to plot the function $\displaystyle \sum_{n=1}^\infty \frac{1}{n^2} H\left(x - \frac{1}{n}\right)$ to illustrate some concepts in real analysis. This is in a response to another question on math.stackexchange:

Here was is example with accompanying plot:

B = [ [1,n] for n in range(1,50) ]

N = 1000
y = np.zeros(N)
count = 1
for b in B:
    x = np.zeros(N)
    x[ np.arange(b[0]*1.0*N/b[1], N).astype(int)]=count**-2
    y += x
    count += 1

plt.plot(np.arange(0,1,1.0/N),y)
plt.axis("Equal")

Heaviside function

** How can I remove the vertical lines and really show the discontinuity? **

$\endgroup$
  • $\begingroup$ I edited my answer to show how to plot your specific function using np.nans. Would you mind reviewing the existing answers and selecting a good one? Thanks! $\endgroup$ – astrojuanlu May 4 '14 at 14:39
3
$\begingroup$

As long as you know the exact positions of the discontinuities, you just have to set the jump positions to nan in x, y or both. You can set this manually in the desired positions or use some criteria - for example, you can use the np.diff function to calculate the difference between contiguous positions in an array. For the simple case of the Heaviside function:

def heaviside(x):
    """See http://stackoverflow.com/a/15122658/554319"""
    y = 0.5 * (np.sign(x) + 1)
    y[np.diff(y) >= 0.5] = np.nan
    return y

x = np.linspace(-1, 1)
plt.plot(x, heaviside(x))
plt.ylim(-1, 2)

Heaviside function with matplotlib

Using this in your specific example:

def U(x, n):
    return sum([heaviside(x - 1 / i) / i ** 2 for i in range(1, n)])

N = 1000
x = np.linspace(0, 1, N)
plt.plot(x, U(x, 50))

OP function

$\endgroup$
  • 2
    $\begingroup$ Can you also get the filled-in and empty circles respectively? See the image in the question: there needs to be a filled circle at (0, 0.5), and empty ones at (0,0) and (0,1). $\endgroup$ – ShreevatsaR Jan 17 '15 at 16:45
2
$\begingroup$

Getting rid of the vertical lines in this plot would involve finding all the discontinuities and then plotting each segment in a separate call to plt.plot.

Another, less elegant, solution is to plot points instead of lines. Since the points are very close together (high value of N), they will look like they form a line. To do this, replace plt.plot(np.arange(0,1,1.0/N), y) with plt.plot(np.arange(0,1,1.0/N), y, '.k') (the .tells plot to use dots, k is for black). This is what you'll get:

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Can you also get the filled-in and empty circles respectively? See the image in the question: there needs to be a filled circle at (0, 0.5), and empty ones at (0,0) and (0,1). $\endgroup$ – ShreevatsaR Jan 17 '15 at 16:45
0
$\begingroup$

You can ' hold ' the plot and plot each component separately. Loop over the different functions and plot.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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