1

I've got this data:

mentor_cnt mentee_cnt
0 3 3
1 3 3
2 7 7
3 18 19
4 24 27
5 37 35
6 53 56
7 63 70
8 86 89
9 102 114
10 135 149
11 154 169
12 174 202
13 232 287
14 298 386
15 343 475
16 384 552
17 446 684
18 509 883
19 469 757

This is my code:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('darkgrid')

fig, ax = plt.subplots(figsize=(10, 6))
sns.lineplot(x=df['month'], y=df['mentor_cnt'], color = 'b', label='Mentors', markers=True, marker='o')
sns.lineplot(x=df['month'], y=df['mentee_cnt'], color = 'r', label='Mentee', markers=True, marker='o')
plt.ylim(min(df['mentee_cnt'])-60, max(df['mentee_cnt'])+50)
columns =['mentor_cnt', 'mentee_cnt']
colors = ['b', 'w']
facecolors = ['none', 'r']
x_position = [0, -10]
y_position = [-18, 5]

for col, fc, c, x_p, y_p in zip(columns, facecolors, colors, x_position, y_position):
    for x, y in zip(df['month'], df[col]):
        label = '{:.0f}'.format(y)
        plt.annotate(
            label,
            (x, y),
            textcoords='offset points',
            xytext=(x_p, y_p),
            ha='center',
            color=c
        ).set_bbox(dict(facecolor=fc, alpha=0.5, boxstyle='round', edgecolor='none'))
ax.legend()
plt.xlabel('Month')
plt.ylabel('Number of persons')
plt.title('Mentee, mentor')
plt.show()

Output: plt.show

Line with mentors looks fine for me, but I can't figure out how to make values for mentee looks better. Is there any way to set best positions of values automatically? With set_bbox I tried to make it readable, but I think it's look terrible.

I improved my graph this way, maybe it'll help someone (thx to trenton-mckinney):

fig, ax = plt.subplots(figsize=(10, 6))
sns.lineplot(x=df['month'], y=df['mentor_cnt'], color='b', label='Mentors', markers=True, marker='o')
sns.lineplot(x=df['month'], y=df['mentee_cnt'], color='r', label='Mentee', markers=True, marker='o')
plt.ylim(min(df['mentee_cnt'])-60, max(df['mentee_cnt'])+50)
columns = ['mentor_cnt', 'mentee_cnt']
colors = ['b', 'r']
facecolors = ['none', 'r']
x_position = [0, -15]
y_position = [-18, 3]

for col, fc, c, x_p, y_p in zip(columns, facecolors, colors, x_position, y_position):
    for x, y in zip(df['month'], df[col]):
        # describe_nearest - list with quantiles for data
        lst = [0, 0.25, 0.5, 0.75, 1]
        describe_nearest = []
        [describe_nearest.append(df[col].quantile(el, interpolation='nearest')) for el in lst]
        describe_nearest.append(df[col].values[-1::][0])
        # add annotation if value in quantiles list
        if y in describe_nearest:
            label = '{:.0f}'.format(y)
            plt.annotate(
                label,
                (x, y),
                textcoords='offset points',
                xytext=(x_p, y_p),
                ha='center',
                color=c
            )
ax.legend()
plt.xlabel('Month')
plt.ylabel('Number of persons')
plt.title('Mentee, mentor')
plt.show()

Output: new output

John Doe
  • 95
  • 6

0 Answers0