Reputation: 982
I have panda dataframe as df with two attributes df.one (=x) and df.two (=y). Now, I want to plot scatter plot for these data points. I used
ax1 = fig.add_subplot(111)
ax1.scatter(df.one,df.two,c = 'g',marker = 'o',alpha = 0.2)
Now, I want to plot centroid of the data points give by C. How should I overlay centroid on the above scatter plot? I tried:
ax1.scatter(C[:,0],C[:,1],c = 'r',marker = 'x')
But it overrides the scatter plot, I want to overlay on that. Is there any hold on option, similar to matlab
?
Upvotes: 8
Views: 55300
Reputation: 319
from matplotlib import pyplot as plt
from statistics import *
bill = [34.00, 108.00, 64.00, 88.00, 99.00, 51.00]
tip = [ 5.00, 17.00, 11.00, 8.00, 14.00, 5.00]
bill.sort()
tip.sort()
print(mean(bill))
print(mean(tip))
plt.scatter(bill, tip)
plt.scatter([mean(bill)], [mean(tip)])
plt.show()
I wanted to plot the mean of the data too, so I used this format and got this result:
Upvotes: 3
Reputation: 3358
If you need points overlaid on the original plot, use
ax.plot(x, y)
ex.
ax = plt.subplot(1, 1, 1)
ax.scatter([1, 2, 3], [1, 2, 3])
ax.plot(1.5, 1.5, "or")
if you pass a list to x and y, multiple points can be added to the plot. Also in case you need to add some annotation beside the point, try
ax.annotate("Some explanation", x, y)
Upvotes: 13