Show 2 histograms on the same plot with 1 calculated histogram in Python

31 views Asked by At

I have created the following sample dataset:

sample_df = pd.DataFrame({
    'user_id':np.arange(0,1000),
    'sent_click_diff':np.random.randint(400, size=1000),
    'clicked_or_not':np.random.choice( ['clicked','not clicked'], 1000),
    })

And here's a sample code to display the % of users on the Y axis:

stepsize = 10
x_axis_values = np.arange(1, 400, stepsize)
#create histogram, using percentages instead of counts
plt.hist(sample_df['sent_click_diff'], weights=np.ones(len(sample_df)) / len(sample_df), bins=x_axis_values)

#apply percentage format to y-axis
plt.gca().yaxis.set_major_formatter(PercentFormatter(1))
plt.xlim(1,400)
plt.title('Distribution of number of days since last click')
plt.xlabel('Number of days since last click')
plt.ylabel('% users')
plt.show()

It gives me the following chart: enter image description here

I would like % clicked for each bin on a Y2 axis in a different color on the same plot. How can I do that? In particular, how do I calculate % clicked for each bin?

TIA for any help.

0

There are 0 answers