Hunter Tipton
Hunter Tipton

Reputation: 333

Pyplot scatterplot interfering with line

So I have a list of numbers as well as the mean and standard deviation of this list. I am attempting to plot the numbers as x values and then plot the lists standard distribution on the same graph however they seem to be interfering with one another. Individually they work fine.

plt.scatter(class1, [0]*len(class1), marker="x", label="C1", c="black")
plt.xlabel('X')
plt.ylabel('P(X|C)')
plt.title('X vs P(X|C)')
plt.legend()
x = np.linspace(mean1 - 3*std1, mean1 + 3*std1, 100)
plt.plot(x, scipy.stats.norm.pdf(x, mean1, std1))
plt.show()

On the same graph: Same Graph

Individually: Distribution

enter image description here

Ideally since they cover the same x values the two graphs would just overlap, however they seem to be being pushed to the side.

Upvotes: 2

Views: 81

Answers (1)

tel
tel

Reputation: 13999

Your code works:

import matplotlib.pyplot as plt
import scipy.stats

class1 = np.random.uniform(-2.4, -1, 100)
mean1 = class1.mean()
std1 = class1.std()

plt.scatter(class1, [0]*len(class1), marker="x", label="C1", c="black")
plt.xlabel('X')
plt.ylabel('P(X|C)')
plt.title('X vs P(X|C)')
plt.legend()
x = np.linspace(mean1 - 3*std1, mean1 + 3*std1, 100)
plt.plot(x, scipy.stats.norm.pdf(x, mean1, std1))
plt.show()

Output:

enter image description here

The likely explanation for your problem then is that somehow in your code mean1 is not actually the the mean of the list of x values in class1.

Upvotes: 2

Related Questions