Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Import necessary libraries for the plot
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- from matplotlib.colors import LinearSegmentedColormap
- import pandas as pd
- import matplotlib.pyplot as plt
- import numpy as np
- from scipy.interpolate import make_interp_spline
- from matplotlib.colors import LinearSegmentedColormap
- # Given productivity scores data
- scores = {
- 2016: {9: 62, 10: 63, 11: 68, 12: 73},
- 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},
- 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},
- 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},
- 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},
- 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},
- 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},
- 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},
- 2024: {1: 89, 2: 80, 3: 68, 4: 82, 5: 91, 6: 58, 7: 63, 8: 44, 9: 58, 10: 91, 11: 69}
- }
- # Plotting
- plt.figure(figsize=(40, 10))
- # Add vertical lines to separate years
- plt.vlines(
- x=[4, 16, 28, 40, 52, 64, 76],
- ymin=0,
- ymax=100,
- colors="#eba434",
- linestyles="dashed",
- label="Year Separators",
- )
- all_scores = [scores[year][month] for year in scores for month in scores[year]]
- dates = [f"{month}/{year}" for year in scores for month in scores[year]]
- xpoints = np.array(list(range(len(all_scores))))
- # Create a Pandas DataFrame
- df = pd.DataFrame({"Date": dates, "Score": all_scores})
- # Calculate the moving average with a window size of 3
- df["Moving_Avg"] = df["Score"].rolling(window=3).mean()
- # Calculate the standard deviation with the same window size
- df["Std_Dev"] = df["Score"].rolling(window=3).std()
- # Calculate the upper and lower bounds for the confidence interval
- df["Upper_Bound"] = df["Moving_Avg"] + (2 * df["Std_Dev"])
- df["Lower_Bound"] = df["Moving_Avg"] - (2 * df["Std_Dev"])
- # Create more points for a smoother curve
- xnew = np.linspace(xpoints.min(), xpoints.max(), 300)
- # Interpolation for original scores
- spl = make_interp_spline(xpoints, df["Score"], k=3)
- y_smooth = spl(xnew)
- # Interpolation for confidence intervals
- spl_upper = make_interp_spline(
- xpoints[~np.isnan(df["Upper_Bound"])], df["Upper_Bound"].dropna(), k=3
- )
- y_upper_smooth = spl_upper(xnew)
- spl_lower = make_interp_spline(
- xpoints[~np.isnan(df["Lower_Bound"])], df["Lower_Bound"].dropna(), k=3
- )
- y_lower_smooth = spl_lower(xnew)
- # Define the colormap to transition from red to blue
- colors = [(1, 0, 0), (0, 0, 1)] # Red to blue
- n_bins = 100
- cmap_name = "red_to_blue"
- colormap = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bins)
- # Plot the original scores with gradient color
- norm = plt.Normalize(y_smooth.min(), y_smooth.max())
- for i in range(len(y_smooth) - 1):
- plt.plot(
- xnew[i : i + 2], y_smooth[i : i + 2], c=colormap(norm(y_smooth[i])), linewidth=2
- )
- # Add a colorbar for original scores
- sm = plt.cm.ScalarMappable(cmap=colormap, norm=norm)
- sm.set_array([])
- plt.colorbar(
- sm,
- ax=plt.gca(), # Explicitly use the current axes
- ticks=np.linspace(0, 100, 11),
- boundaries=np.arange(-0.05, 100.1, 0.1),
- label="Original Scores",
- )
- # Plot the confidence intervals with interpolation for smoothness
- plt.fill_between(
- xnew,
- y_upper_smooth,
- y_lower_smooth,
- color="grey",
- alpha=0.5,
- label="Confidence Interval (Smoothed)",
- )
- plt.xticks(ticks=xpoints, labels=dates, rotation=90)
- plt.yticks(range(0, 101, 10))
- plt.ylim(0, 100)
- plt.title("Productivity Scores Over the Years (Smoothed, With Gradient Color)")
- plt.xlabel("Months")
- plt.ylabel("Productivity Score")
- plt.grid(axis="y")
- plt.legend()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement