LivBanana
LivBanana

Reputation: 381

Why there is no plot generated?

I want to plot the following

from matplotlib import pyplot as plt
import numpy as np
N = 1000000
for k in range(1, 2000):
    plt.plot(k, 1 - np.exp(-0.5 * k * (k - 1) / N))
plt.show()

But there output is just the axis

enter image description here

Why is that?

Upvotes: 0

Views: 51

Answers (2)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31153

What exactly do you want to have as a result? Currently your code does 2000 plots and by default they don't show anything since each has only one point and default is a line graph. If you want to plot a single line graph from 1 to 2000 then you should use this:

from matplotlib import pyplot as plt
import numpy as np
N = 1000000
plt.plot(range(1, 2000), [1 - np.exp(-0.5 * k * (k - 1) / N) for k in range(1, 2000)])
plt.show()

It creates a single plot with x going from 1 to 2000 and y getting values with the formula also from 1 to 2000.

If you do want 2000 separate plots then you can define the style for plot(), for example 's' as the last parameter, or if you want a single color for each point you can add that too, for example 'ks' for black.

Upvotes: 0

mhhabib
mhhabib

Reputation: 3121

You're not showing anything in the plot because of you didn't set any color parameter. Set a color parameter and you will the output. For demonstration I set a blue color code.

plt.plot(k, 1 - np.exp(-0.5 * k * (k - 1) / N), 'bs')

enter image description here

Upvotes: 1

Related Questions