Skip to content

Instantly share code, notes, and snippets.

@felipessalvatore
Last active October 27, 2017 19:02
Show Gist options
  • Save felipessalvatore/9dd7d13225550c0081387c85f472c94f to your computer and use it in GitHub Desktop.
Save felipessalvatore/9dd7d13225550c0081387c85f472c94f to your computer and use it in GitHub Desktop.
Plot points with numerical label in seaborn and latex title
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import rc
sns.set_style("darkgrid")
# for using latex in plt it requires one installation:
# $ sudo apt install dvipng
rc('font', **{'family': 'sans-serif', 'sans-serif': ['Helvetica']})
rc('font', **{'family': 'serif', 'serif': ['Palatino']})
rc('text', usetex=True)
def plot_runners(lab, year, names, position_per_cat):
"""
plot the results of all the students
using their positions
:type lab: str
:type year: str
:type names: [str]
:type position_per_cat: np.array
"""
# calculating scores
scores = - np.log(position_per_cat)
tuples = list(zip(scores, names))
tuples.sort()
scores = []
names = []
for car, cdr in tuples:
scores.append(car)
names.append(cdr)
scores_label = [round(i, 3) for i in scores]
scores_mean = np.mean(scores)
scores_std = np.std(scores)
# ploting
fig, ax = plt.subplots(figsize=(13, 10), sharex=True)
ax = sns.pointplot(x=names, y=scores, join=False)
plt.xlabel('Student', fontsize=18, fontweight='bold')
plt.ylabel('Score', fontsize=18, fontweight='bold')
title_car1 = "Running score of the {} ".format(lab)
title_car2 = "students at 'volta da USP {}'\n".format(year)
title_car = title_car1 + title_car2
title_cdr = r"$\displaystyle score = -\log\left(\frac{position}{\#\,category}\right)$"
fig.suptitle(title_car + title_cdr,
fontsize=20,
fontweight='bold',
y=0.99)
bbox_props = dict(boxstyle="square,pad=0.3", fc="white", ec="black", lw=0.2)
box_position_x, box_position_y = ax.get_xticks()[0], scores[-1]
ax.text(box_position_x,
box_position_y,
"$mean = {:.3f}, std = {:.3f}$".format(scores_mean, scores_std),
size=15,
bbox=bbox_props)
for point in zip(ax.get_xticks(), scores_label):
ax.text(point[0], point[1] + 0.05, point[1], color='g')
# saving
filename = "VoltaUSP" + year + lab + ".png"
plt.savefig(filename)
# inputs
year = '2017'
lab = "IME"
names_liamf = ['Erika', "Felipe", 'Lucas M.', "Thiago", "Paula", 'Willy']
position_liamf = np.array([45 / 118,
21 / 93,
38 / 158,
137 / 158,
53 / 202,
8 / 158])
names_music = ["Oberlan", " Shayenne"]
position_music = np.array([5 / 74, 174 / 202])
names_system = ["Lucas D.", "Dylan", "Rodrigo"]
position_system = np.array([33 / 131, 80 / 158, 10 / 131])
all_names = np.concatenate([names_liamf, names_music, names_system], 0)
all_positions = np.concatenate([position_liamf, position_music, position_system], 0)
plot_runners(lab, year, all_names, all_positions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment