Reputation: 25
I have written the below code in the seaborn scatterplot for the visual to create
sns.scatterplot(x="Year_Birth", y="NumStorePurchases", data=md)
I get the plot in the format below
I now need the average of the Y- axis and want to adjust the plot to look like the below picture
I have tried using the np.mean in the estimator but it has not worked out. Is there something that is being wrongly done? How this can be achieved?
Upvotes: 1
Views: 1081
Reputation: 80329
If you only want one y-value for each x-value, you can create a sns.pointplot(...,orient='v')
. Note that a pointplot uses a categorical axis, considering the x-values as strings, internally numbered 0,1,2,...
. By default, each of these x-values will be labeled. A MultipleLocator
can be used to only show e.g. every 10.
To suppress lines joining all the values, use join=False
, while ci=None
suppresses the error lines of the confidence intervals.
Here is an example:
from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator
import seaborn as sns
import pandas as pd
import numpy as np
md = pd.DataFrame({"Year_Birth": np.random.randint(1940, 2000, 200),
"NumStorePurchases": np.random.randint(0, 14, 200)})
plt.figure(figsize=(12, 4))
sns.set_style('darkgrid')
ax = sns.pointplot(x="Year_Birth", y="NumStorePurchases", order=range(1900, 2020),
orient='v', join=False, ci=None, color='cornflowerblue', data=md)
ax.xaxis.set_major_locator(MultipleLocator(10))
plt.tight_layout()
plt.show()
Upvotes: 2