Commit 9007704c4b
Unsigned
Layout: unified · split
.gitignore +1
| @@ -3,3 +3,4 @@ | |||
| 3 | .vscode | 3 | .vscode |
| 4 | public | 4 | public |
| 5 | readme.html | 5 | readme.html |
| 6 | venv | ||
utils/salary_visualization.py +20 −17
| @@ -1,8 +1,10 @@ | |||
| 1 | %pip install plotly pandas | 1 | """ |
| 2 | A file that will visualize salary data from an incoming CSV. | ||
| 3 | """ | ||
| 2 | 4 | ||
| 5 | import locale | ||
| 3 | import pandas as pd | 6 | import pandas as pd |
| 4 | import plotly.graph_objs as go | 7 | import plotly.graph_objs as go |
| 5 | import locale | ||
| 6 | 8 | ||
| 7 | locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') | 9 | locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') |
| 8 | 10 | ||
| @@ -10,6 +12,9 @@ df = pd.read_csv('~/git/cleberg.net/static/salary.csv') | |||
| 10 | 12 | ||
| 11 | # Function to format salary as US currency | 13 | # Function to format salary as US currency |
| 12 | def format_currency(value): | 14 | def format_currency(value): |
| 15 | """ | ||
| 16 | Format values in USD currency format. | ||
| 17 | """ | ||
| 13 | return f"${value:,.2f}" | 18 | return f"${value:,.2f}" |
| 14 | 19 | ||
| 15 | # Reverse the order of the DataFrame | 20 | # Reverse the order of the DataFrame |
| @@ -23,7 +28,7 @@ fig = go.Figure() | |||
| 23 | 28 | ||
| 24 | # Adding each data point as a separate trace to display the text | 29 | # Adding each data point as a separate trace to display the text |
| 25 | for index, row in df.iterrows(): | 30 | for index, row in df.iterrows(): |
| 26 | title_company = f"{row['Title']} ({row['Company']})" | 31 | TITLE_COMPANY = f"{row['Title']} ({row['Company']})" |
| 27 | salary_formatted = format_currency(row['Salary']) | 32 | salary_formatted = format_currency(row['Salary']) |
| 28 | if pd.notna(row['Percentage Increase']): | 33 | if pd.notna(row['Percentage Increase']): |
| 29 | text = f"{salary_formatted} ({row['Percentage Increase']:.2f}%)" | 34 | text = f"{salary_formatted} ({row['Percentage Increase']:.2f}%)" |
| @@ -34,27 +39,25 @@ for index, row in df.iterrows(): | |||
| 34 | y=[row['Salary'], row['Salary']], | 39 | y=[row['Salary'], row['Salary']], |
| 35 | text=[text], | 40 | text=[text], |
| 36 | mode='lines+text', | 41 | mode='lines+text', |
| 37 | name=title_company, # Combine title and company for legend | 42 | name=TITLE_COMPANY, |
| 38 | textposition='top center' | 43 | textposition='top center' |
| 39 | )) | 44 | )) |
| 40 | 45 | ||
| 46 | # Update visual styles of the figure | ||
| 41 | fig.update_layout( | 47 | fig.update_layout( |
| 42 | title="Salary Data Over Time (annualized)", | 48 | title="Salary Data Over Time (annualized)", |
| 43 | xaxis_title="Time", | 49 | xaxis_title="Time", |
| 44 | yaxis_title="Salary", | 50 | yaxis_title="Salary", |
| 45 | font=dict( | 51 | font={"family": 'monospace', "size": 16}, |
| 46 | family="monospace", | 52 | margin={"l": 50, "r": 50, "t": 50, "b": 100}, |
| 47 | size=16 | 53 | legend={ |
| 48 | ), | 54 | "orientation": 'h', |
| 49 | margin=dict(l=50, r=50, t=50, b=100), # Adjust bottom margin for legend space | 55 | "yanchor": 'top', |
| 50 | legend=dict( | 56 | "y": -0.3, |
| 51 | orientation="h", # Horizontal orientation | 57 | "xanchor": 'center', |
| 52 | yanchor="top", # Anchor to the top of the legend box | 58 | "x": 0.5 |
| 53 | y=-0.3, # Position below the graph | 59 | }, |
| 54 | xanchor="center", # Center the legend horizontally | 60 | height=800 |
| 55 | x=0.5 # Center the legend | ||
| 56 | ), | ||
| 57 | height=800 # Increase vertical size | ||
| 58 | ) | 61 | ) |
| 59 | 62 | ||
| 60 | fig.show() | 63 | fig.show() |