Advertisement
TheDemystifier

RescueT Produc Plot.ipynb

Dec 30th, 2023 (edited)
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 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: 90},
  22.     2024: {1: 89, 2: 80, 3: 68, 4: 82, 5: 91, 6: 58, 7: 63, 8: 44, 9: 58, 10: 91, 11: 69}
  23. }
  24.  
  25. # Plotting
  26. plt.figure(figsize=(40, 10))
  27.  
  28. # Add vertical lines to separate years
  29. plt.vlines(
  30.     x=[4, 16, 28, 40, 52, 64, 76],
  31.     ymin=0,
  32.     ymax=100,
  33.     colors="#eba434",
  34.     linestyles="dashed",
  35.     label="Year Separators",
  36. )
  37.  
  38. all_scores = [scores[year][month] for year in scores for month in scores[year]]
  39. dates = [f"{month}/{year}" for year in scores for month in scores[year]]
  40. xpoints = np.array(list(range(len(all_scores))))
  41.  
  42. # Create a Pandas DataFrame
  43. df = pd.DataFrame({"Date": dates, "Score": all_scores})
  44.  
  45. # Calculate the moving average with a window size of 3
  46. df["Moving_Avg"] = df["Score"].rolling(window=3).mean()
  47.  
  48. # Calculate the standard deviation with the same window size
  49. df["Std_Dev"] = df["Score"].rolling(window=3).std()
  50.  
  51. # Calculate the upper and lower bounds for the confidence interval
  52. df["Upper_Bound"] = df["Moving_Avg"] + (2 * df["Std_Dev"])
  53. df["Lower_Bound"] = df["Moving_Avg"] - (2 * df["Std_Dev"])
  54.  
  55. # Create more points for a smoother curve
  56. xnew = np.linspace(xpoints.min(), xpoints.max(), 300)
  57.  
  58. # Interpolation for original scores
  59. spl = make_interp_spline(xpoints, df["Score"], k=3)
  60. y_smooth = spl(xnew)
  61.  
  62. # Interpolation for confidence intervals
  63. spl_upper = make_interp_spline(
  64.     xpoints[~np.isnan(df["Upper_Bound"])], df["Upper_Bound"].dropna(), k=3
  65. )
  66. y_upper_smooth = spl_upper(xnew)
  67.  
  68. spl_lower = make_interp_spline(
  69.     xpoints[~np.isnan(df["Lower_Bound"])], df["Lower_Bound"].dropna(), k=3
  70. )
  71. y_lower_smooth = spl_lower(xnew)
  72.  
  73. # Define the colormap to transition from red to blue
  74. colors = [(1, 0, 0), (0, 0, 1)]  # Red to blue
  75. n_bins = 100
  76. cmap_name = "red_to_blue"
  77. colormap = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bins)
  78.  
  79. # Plot the original scores with gradient color
  80. norm = plt.Normalize(y_smooth.min(), y_smooth.max())
  81. for i in range(len(y_smooth) - 1):
  82.     plt.plot(
  83.         xnew[i : i + 2], y_smooth[i : i + 2], c=colormap(norm(y_smooth[i])), linewidth=2
  84.     )
  85.  
  86. # Add a colorbar for original scores
  87. sm = plt.cm.ScalarMappable(cmap=colormap, norm=norm)
  88. sm.set_array([])
  89. plt.colorbar(
  90.     sm,
  91.     ax=plt.gca(),  # Explicitly use the current axes
  92.     ticks=np.linspace(0, 100, 11),
  93.     boundaries=np.arange(-0.05, 100.1, 0.1),
  94.     label="Original Scores",
  95. )
  96.  
  97. # Plot the confidence intervals with interpolation for smoothness
  98. plt.fill_between(
  99.     xnew,
  100.     y_upper_smooth,
  101.     y_lower_smooth,
  102.     color="grey",
  103.     alpha=0.5,
  104.     label="Confidence Interval (Smoothed)",
  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