import matplotlib.pyplot as plt import numpy as np def plot_linear_functions(list1, list2, list3, b, x_range=(0, 10)): """ Plots linear functions y = Cx + D where: C = b - k (k elements removed) D = sum of remaining elements k ranges from 0 to b. """ datasets = [list1, list2, list3] # Distinct colors for the 3 sets colors = ['#E63946', '#457B9D', '#1D3557'] labels = ['List 1', 'List 2', 'List 3'] plt.figure(figsize=(12, 8)) x = np.linspace(x_range[0], x_range[1], 100) for i, current_list in enumerate(datasets): # Sort descending so the first k elements are the 'top k' sorted_list = sorted(current_list, reverse=True) for k in range(len(sorted_list) + 1): # C is the fixed number b minus the number of elements removed C = k-b # Remove top k elements (if k > list length, remaining is empty) remaining = sorted_list[k:] if k < len(sorted_list) else [] # D is the sum of the remaining elements D = sum(remaining) y = C * x + D # Plot the line; label only once per list for a clean legend line_label = labels[i] if k == 0 else "" if C == 0: plt.plot(x, y, color=colors[i], alpha=1, label=line_label, linestyle='-') else: plt.plot(x, y, color=colors[i], alpha=0.6, label=line_label, linestyle='--') plt.title( f"2D Linear Functions: $y = (b-k)x + sum(rem)$ for $0 ≤ k ≤ b$ ($b={b}$)") plt.xlabel("x") plt.ylabel("y") plt.grid(True, linestyle='--', alpha=0.7) plt.legend() plt.savefig('linear_plot.png') plt.show() # --- Example Usage --- # Replace these with your actual lists and b value L1 = [45, 14, 7, 7, 4, 2] L2 = [20, 15, 10, 5, 4, 2] L3 = [5, 5, 5, 5, 5, 5] fixed_b = 2 plot_linear_functions(L1, L2, L3, fixed_b)