Advertisement
TheDemystifier

RescueT Produc Plot.ipynb

Dec 30th, 2023 (edited)
1,045
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.81 KB | Source Code | 0 0
  1. # Import necessary libraries for the plot
  2. import numpy as np
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5. from matplotlib.colors import LinearSegmentedColormap
  6. import pandas as pd
  7. import matplotlib.pyplot as plt
  8. import numpy as np
  9. from scipy.interpolate import make_interp_spline
  10. from matplotlib.colors import LinearSegmentedColormap
  11.  
  12. # Given productivity scores data
  13. scores = {
  14.     2016: {9: 62, 10: 63, 11: 68, 12: 73},
  15.     2017: {1: 71, 2: 70, 3: 79, 4: 78, 5: 70, 6: 77, 7: 74, 8: 64, 9: 65, 10: 62, 11: 54, 12: 49},
  16.     2018: {1: 44, 2: 65, 3: 57, 4: 49, 5: 54, 6: 45, 7: 33, 8: 36, 9: 48, 10: 58, 11: 76, 12: 81},
  17.     2019: {1: 70, 2: 68, 3: 71, 4: 84, 5: 79, 6: 78, 7: 68, 8: 62, 9: 73, 10: 83, 11: 79, 12: 79},
  18.     2020: {1: 69, 2: 70, 3: 58, 4: 46, 5: 64, 6: 60, 7: 75, 8: 49, 9: 71, 10: 75, 11: 68, 12: 61},
  19.     2021: {1: 67, 2: 66, 3: 45, 4: 50, 5: 50, 6: 54, 7: 61, 8: 48, 9: 54, 10: 59, 11: 56, 12: 54},
  20.     2022: {1: 49, 2: 46, 3: 53, 4: 56, 5: 45, 6: 42, 7: 49, 8: 39, 9: 51, 10: 61, 11: 54, 12: 52},
  21.     2023: {1: 82, 2: 85, 3: 77, 4: 78, 5: 71, 6: 60, 7: 23, 8: 43, 9: 66, 10: 91, 11: 90, 12: 89},
  22. }
  23.  
  24. # Plotting
  25. plt.figure(figsize=(40, 10))
  26.  
  27. # Add vertical lines to separate years
  28. plt.vlines(
  29.     x=[4, 16, 28, 40, 52, 64, 76],
  30.     ymin=0,
  31.     ymax=100,
  32.     colors="#eba434",
  33.     linestyles="dashed",
  34.     label="Year Separators",
  35. )
  36.  
  37. all_scores = [scores[year][month] for year in scores for month in scores[year]]
  38. dates = [f"{month}/{year}" for year in scores for month in scores[year]]
  39. xpoints = np.array(list(range(len(all_scores))))
  40.  
  41. # Create a Pandas DataFrame
  42. df = pd.DataFrame({"Date": dates, "Score": all_scores})
  43.  
  44. # Calculate the moving average with a window size of 3
  45. df["Moving_Avg"] = df["Score"].rolling(window=3).mean()
  46.  
  47. # Calculate the standard deviation with the same window size
  48. df["Std_Dev"] = df["Score"].rolling(window=3).std()
  49.  
  50. # Calculate the upper and lower bounds for the confidence interval
  51. df["Upper_Bound"] = df["Moving_Avg"] + (2 * df["Std_Dev"])
  52. df["Lower_Bound"] = df["Moving_Avg"] - (2 * df["Std_Dev"])
  53.  
  54. # Create more points for a smoother curve
  55. xnew = np.linspace(xpoints.min(), xpoints.max(), 300)
  56.  
  57. # Interpolation for original scores
  58. spl = make_interp_spline(xpoints, df["Score"], k=3)
  59. y_smooth = spl(xnew)
  60.  
  61. # Interpolation for confidence intervals
  62. spl_upper = make_interp_spline(
  63.     xpoints[~np.isnan(df["Upper_Bound"])], df["Upper_Bound"].dropna(), k=3
  64. )
  65. y_upper_smooth = spl_upper(xnew)
  66.  
  67. spl_lower = make_interp_spline(
  68.     xpoints[~np.isnan(df["Lower_Bound"])], df["Lower_Bound"].dropna(), k=3
  69. )
  70. y_lower_smooth = spl_lower(xnew)
  71.  
  72. # Define the colormap to transition from red to blue
  73. colors = [(1, 0, 0), (0, 0, 1)]  # Red to blue
  74. n_bins = 100
  75. cmap_name = "red_to_blue"
  76. colormap = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bins)
  77.  
  78. # Plot the original scores with gradient color
  79. norm = plt.Normalize(y_smooth.min(), y_smooth.max())
  80. for i in range(len(y_smooth) - 1):
  81.     plt.plot(
  82.         xnew[i : i + 2], y_smooth[i : i + 2], c=colormap(norm(y_smooth[i])), linewidth=2
  83.     )
  84.  
  85. # Add a colorbar for original scores
  86. sm = plt.cm.ScalarMappable(cmap=colormap, norm=norm)
  87. sm.set_array([])
  88. plt.colorbar(
  89.     sm,
  90.     ticks=np.linspace(0, 100, 11),
  91.     boundaries=np.arange(-0.05, 100.1, 0.1),
  92.     label="Original Scores",
  93. )
  94.  
  95.  
  96. # Plot the confidence intervals with interpolation for smoothness
  97. plt.fill_between(
  98.     xnew,
  99.     y_upper_smooth,
  100.     y_lower_smooth,
  101.     color="grey",
  102.     alpha=0.5,
  103.     label="Confidence Interval (Smoothed)",
  104. )
  105.  
  106.  
  107. plt.xticks(ticks=xpoints, labels=dates, rotation=90)
  108. plt.yticks(range(0, 101, 10))
  109. plt.ylim(0, 100)
  110. plt.title("Productivity Scores Over the Years (Smoothed, With Gradient Color)")
  111. plt.xlabel("Months")
  112. plt.ylabel("Productivity Score")
  113. plt.grid(axis="y")
  114. plt.legend()
  115.  
  116. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement