Skip to content Skip to sidebar Skip to footer

Matplotlib Histogram Time Vs. Percentage (nba Stats)

I'm having an issue and I'm trying to wrap my head around it. Very new at pandas/matplotlib. I want to show a histogram with the shot clock (0-24sec) in bins on the X-axis and the

Solution 1:

The relative frequency of the events in the bins would be obtained by dividing the absolute frequency by the total number of events.

You would therefore need to calculate the histogram, e.g. with numpy

hist, bins = np.histogram(x)

Depending on whether you then divide by the number of events within each bin, or the number of total events you can get different plots. From the one on the left hand side you can easily grasp that e.g. the hit rate is higher for a larger clock time (this may not make sense for the real data of course). From the plot on the right you would rather grasp that more trials were made for medium clock times - something that is not seen at all if you only show the relative hits.

enter image description here

from __future__ import division
import pandas as pd
import numpy as np; np.random.seed(2)
import matplotlib.pyplot as plt

t = np.random.rand(100)*24
hit = np.random.randint(0,2, size=100)
df = pd.DataFrame({"time":t, "hits":hit})
df_miss=df[df.hits == 0]
df_hits=df[df.hits == 1]

bins=np.arange(0,28,4)
hist_hits, bins_ = np.histogram(df_hits.time, bins=bins)
hist_miss, bins_ = np.histogram(df_miss.time, bins=bins)

rel_hits = hist_hits/(hist_hits+hist_miss)*100.
rel_miss = hist_miss/(hist_hits+hist_miss)*100.

rel_hits_n = hist_hits/np.sum(hist_hits+hist_miss)*100.
rel_miss_n = hist_miss/np.sum(hist_hits+hist_miss)*100.


fig , (ax, ax2) = plt.subplots(ncols=2, figsize=(7,3))

ax.bar(bins[:-1], rel_hits, width=4,  
       color="mediumseagreen", align="edge", ec="k")
ax.bar(bins[:-1], rel_miss,  bottom=rel_hits, width=4, 
       color="tomato", align="edge", ec="k")
ax.set_xticks(bins)
ax.set_ylabel("relative hits and misses [%]")
ax2.bar(bins[:-1], rel_hits_n, width=4,  
        color="mediumseagreen", align="edge", ec="k", label="hit")
ax2.bar(bins[:-1], rel_miss_n,  bottom=rel_hits_n, width=4, 
        color="tomato", align="edge", ec="k", label="miss")
ax2.set_xticks(bins)
ax2.set_ylabel("normalized hits and misses [%]")

plt.legend()
plt.tight_layout()
plt.show()

Post a Comment for "Matplotlib Histogram Time Vs. Percentage (nba Stats)"