Expected2BlankLines
Expected2BlankLines

Reputation: 55

How do I make my plot look like this with matplotlib?

So right now I'm trying to simulate a Poisson process for an assignment, here's the code so far:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

y = np.arange(0,21,1)
x = np.cumsum(np.random.exponential(2,21))

print(y)
print(x)

sns.set()
plt.plot(x,y)
plt.show()

The problem arises when I try plotting it. The code above, as expected, produces a normal matplotlib plot that looks like this:

enter image description here

However I need it to look like this:

enter image description here

Is there an easy way of doing it? I tried messing with bar plots but was unable to produce something that looks good.

Upvotes: 0

Views: 229

Answers (1)

Ashwin Geet D'Sa
Ashwin Geet D'Sa

Reputation: 7369

The graph that you are wanting to plot is called as step plot in matplotlib. In order to plot it replace plt.plot(x,y) with plt.step(x,y)

So, your code becomes:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

y = np.arange(0,21,1)
x = np.cumsum(np.random.exponential(2,21))

print(y)
print(x)

sns.set()
plt.step(x,y)
plt.show()

Upvotes: 1

Related Questions