Example output
import pandas as pd
data = {
"HITTERS": ["M. Betts", "aJ. Outman", "F. Freeman", "A. Barnes", "Y. Fernandez", "W. Smith", "L. Williams", "J.D. Martinez", "D. Avans", "M. Muncy", "D. Peralta", "C. Taylor", "E. Leonard", "T. Thompson", "J. Heyward", "M. Vargas", "J. Vivas", "M. Rojas", "K. Nevin"],
"AB": [1, 2, 3, 2, 0, 2, 2, 4, 1, 2, 3, 2, 1, 1, 1, 3, 1, 2, 1],
"R": [1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0],
"H": [1, 0, 2, 0, 0, 1, 0, 3, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0],
"RBI": [1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0],
"HR": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
"BB": [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 1, 0],
"K": [0, 2, 1, 1, 0, 0, 0, 1, 1, 0, 2, 1, 1, 1, 1, 1, 1, 1, 0],
"AVG": [0.278, 0.283, 0.316, 0.182, 0, 0.333, 0.152, 0.232, 0.2, 0.304, 0.143, 0.158, 0.375, 0.086, 0.2, 0.25, 0.238, 0.283, 0],
"OBP": [0.364, 0.391, 0.372, 0.25, 0, 0.346, 0.204, 0.283, 0.342, 0.418, 0.273, 0.294, 0.4, 0.273, 0.273, 0.389, 0.333, 0.353, 0],
"SLG": [0.778, 0.566, 0.605, 0.227, 0, 0.458, 0.391, 0.375, 0.3, 0.587, 0.286, 0.333, 0.542, 0.143, 0.36, 0.386, 0.333, 0.478, 0],
}
df = pd.DataFrame(data)
import matplotlib.pyplot as plt
def plot_top_players(metric, title):
top_players = df.nlargest(5, metric).sort_values(by=metric, ascending=True)
plt.barh(top_players["HITTERS"], top_players[metric])
plt.xlabel(metric)
plt.title(title)
plt.show()
# Top 5 hitters by AVG
plot_top_players("AVG", "Top 5 Hitters by AVG")
# Top 5 hitters by OBP
plot_top_players("OBP", "Top 5 Hitters by OBP")