Skip to content

Instantly share code, notes, and snippets.

@mzaks
Created January 19, 2024 07:28
Show Gist options
  • Save mzaks/b0ad94035ac365812ce5efeedaa182fe to your computer and use it in GitHub Desktop.
Save mzaks/b0ad94035ac365812ce5efeedaa182fe to your computer and use it in GitHub Desktop.
Plot your experience
import matplotlib.pyplot as plt
def _normalized_experience(data: dict[str, list[int | list[int]]]) -> tuple[int, dict[str, list[int]]]:
"""Returns a tuple with latest year and an experience dict with normalised years list"""
latest_year = 0
result = {}
for topic, years in data.items():
normalised_years = []
for entry in years:
if isinstance(entry, list):
assert len(entry) == 2 and entry[0] < entry[1]
years.extend(range(entry[0], entry[1] + 1))
latest_year = entry[1] if latest_year < entry[1] else latest_year
else:
normalised_years.append(entry)
latest_year = entry if latest_year < entry else latest_year
result[topic] = normalised_years
return latest_year, result
def plot_experience(title: str, experience: dict[str, list[int | list[int]]], show_legend: bool = True):
"""Create a scatter plot from experience dictionary.
Args:
title: Title of the plot
experience: A dict where years of experience are grouped by a topic.
The years are combined in a list. A list item can represent a single year, or a list with start and end year.
show_legend: Marks if the legend which reflects the experience item with the total amount of years, should be displayed.
"""
latest_year, norm_exp = _normalized_experience(experience)
fig, ax = plt.subplots()
ind_x = range(latest_year + 1)
ind_y = range(len(norm_exp))
legend = []
for i, topic in enumerate(norm_exp.keys()):
ax.scatter(norm_exp[topic], [i] * len(norm_exp[topic]))
quantity = len(norm_exp[topic])
legend.append(f"{topic}: {quantity} year{'' if quantity < 2 else 's'}")
fig.canvas.manager.set_window_title(title)
ax.set_title(title)
ax.set_xticks(ind_x, labels=list(range(0, latest_year + 1)), rotation=-45)
ax.set_yticks(ind_y, labels=list(norm_exp.keys()))
if show_legend:
ax.legend(legend, bbox_to_anchor=(1.05, 1), loc='upper left')
plt.subplots_adjust(right=0.7, left=0.15)
ax.autoscale_view()
plt.grid(linestyle = '--', linewidth = 0.5)
plt.title(title)
plt.show()
plot_experience(
"Programming Languages",
{
"ActionScript": [2004, 2008, 2009],
"Java": [[2005, 2011], 2022],
"C++": [2007, 2013, 2016, 2023],
"ObjC": [[2008, 2014], 2017],
"JavaScript": [[2011, 2012], 2016, 2020, 2022],
"Xtext/Xtend": [2011, [2016, 2019]],
"Scala": [2012],
"C#": [[2014, 2016], [2019, 2021]],
"Swift": [[2014, 2023]],
"Rust": [2019, 2020],
"Dart": [2019, 2020, 2022, 2023],
"Python": [2020, 2022, 2023],
"Mojo": [2023],
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment