|
1 | 1 | class MultivariatePlotGenerator: |
2 | 2 | @staticmethod |
3 | | - def generate(df, plot, x, y, hue, title, rotation, color, withImport): |
| 3 | + def generate(df, plot_type, params, withImport=False): |
4 | 4 | import_stmts = [ |
5 | 5 | "import matplotlib.pyplot as plt", |
6 | 6 | "import seaborn as sns" |
7 | 7 | ] |
8 | | - code = "" |
| 8 | + code = "plt.figure(figsize=(8, 6))\n" |
9 | 9 |
|
10 | | - title_stmt = f"plt.title(\"{title}\")\n" if title else "" |
11 | | - rot_stmt = f"plt.xticks(rotation={rotation})\n" if rotation != 0 else "" |
12 | | - color_stmt = f", palette='{color}'" if color else "" |
| 10 | + if params.get("Style"): |
| 11 | + code += f"sns.set_style('{params['Style']}')\n" |
13 | 12 |
|
14 | | - match plot: |
15 | | - case "Heatmap": |
16 | | - color_stmt = f", cmap='{color}'" if color else "" |
| 13 | + if params.get("Show Legend") is False: |
| 14 | + legend_stmt = "plt.legend().remove()\n" |
| 15 | + else: |
| 16 | + legend_stmt = "" |
| 17 | + |
| 18 | + if params.get("Grid Lines"): |
| 19 | + code += "plt.grid(True)\n" |
| 20 | + |
| 21 | + palette = f", palette='{params['Palette']}'" if params.get("Palette") else "" |
| 22 | + color = f", color='{params['Color']}'" if params.get("Color") else "" |
| 23 | + cmap = f", cmap='{params['Color Map']}'" if params.get("Color Map") else "" |
| 24 | + marker = f", marker='{params['Marker Style']}'" if params.get("Marker Style") else "" |
| 25 | + linestyle = f", linestyle='{params['Line Style']}'" if params.get("Line Style") else "" |
| 26 | + alpha = f", alpha={params['Alpha (Transparency)']}" if params.get("Alpha (Transparency)") is not None else "" |
| 27 | + |
| 28 | + rotation = params.get("Label Rotation", 0) |
| 29 | + rot_stmt = f"plt.xticks(rotation={rotation})\n" if rotation else "" |
| 30 | + |
| 31 | + title = params.get("Plot Title") |
| 32 | + if title: |
| 33 | + code += f"plt.title('{title}')\n" |
| 34 | + |
| 35 | + xlabel = params.get("X Axis Label") or params.get("X Axis") |
| 36 | + if xlabel: |
| 37 | + code += f"plt.xlabel('{xlabel}')\n" |
| 38 | + |
| 39 | + ylabel = params.get("Y Axis Label") or params.get("Y Axis") |
| 40 | + if ylabel: |
| 41 | + code += f"plt.ylabel('{ylabel}')\n" |
| 42 | + |
| 43 | + |
| 44 | + match plot_type: |
| 45 | + case "Grouped Violin Plot": |
17 | 46 | code += ( |
18 | | - f"correlation_matrix = {df}[{df}.select_dtypes(include='number').columns].corr()\n" |
19 | | - f"plt.figure(figsize=(10, 8))\n" |
20 | | - f"sns.heatmap(correlation_matrix, annot=True{color_stmt}, fmt='.2f', cbar=True)\n" |
21 | | - f"{title_stmt}" |
22 | | - f"plt.show()\n" |
| 47 | + f"sns.violinplot(data={df}, x='{params['X Axis']}', y='{params['Y Axis']}', hue='{params['Hue']}'{palette}, split=True)\n" |
| 48 | + f"{rot_stmt}{legend_stmt}" |
23 | 49 | ) |
24 | 50 |
|
25 | | - case "Pair Plot": |
26 | | - hue_stmt = f", hue='{hue}'{color_stmt}" if hue else "" |
| 51 | + case "Grouped Swarm Plot": |
27 | 52 | code += ( |
28 | | - f"plt.figure(figsize=(10, 8))\n" |
29 | | - f"sns.pairplot({df}.select_dtypes(include='number'), diag_kind='kde'{hue_stmt})\n" |
30 | | - f"{title_stmt}" |
31 | | - f"plt.show()\n" |
| 53 | + f"sns.swarmplot(data={df}, x='{params['X Axis']}', y='{params['Y Axis']}', hue='{params['Hue']}'{palette}{alpha})\n" |
| 54 | + f"{rot_stmt}{legend_stmt}" |
32 | 55 | ) |
33 | 56 |
|
34 | | - case "Violin Plot": |
35 | | - hue_stmt = f", hue='{hue}'{color_stmt}" if hue else "" |
| 57 | + case "Grouped Scatter Plot": |
36 | 58 | code += ( |
37 | | - f"plt.figure(figsize=(8, 6))\n" |
38 | | - f"sns.violinplot(data={df}, x='{x}', y='{y}'{hue_stmt}, split=True)\n" |
39 | | - f"{title_stmt}" |
40 | | - f"{rot_stmt}" |
41 | | - f"plt.show()\n" |
| 59 | + f"sns.scatterplot(data={df}, x='{params['X Axis']}', y='{params['Y Axis']}', hue='{params['Hue']}'{palette}{marker}{alpha})\n" |
| 60 | + f"{rot_stmt}{legend_stmt}" |
42 | 61 | ) |
43 | 62 |
|
44 | | - case "Swarm Plot": |
45 | | - hue_stmt = f", hue='{hue}'{color_stmt}" if hue else "" |
| 63 | + case "Grouped Line Plot": |
46 | 64 | code += ( |
47 | | - f"plt.figure(figsize=(8, 6))\n" |
48 | | - f"sns.swarmplot(data={df}, x='{x}', y='{y}'{hue_stmt})\n" |
49 | | - f"{title_stmt}" |
50 | | - f"{rot_stmt}" |
51 | | - f"plt.show()\n" |
| 65 | + f"sns.lineplot(data={df}, x='{params['X Axis']}', y='{params['Y Axis']}', hue='{params['Hue']}'{marker}{linestyle}{alpha})\n" |
| 66 | + f"{rot_stmt}{legend_stmt}" |
52 | 67 | ) |
53 | 68 |
|
54 | | - case "Scatter Plot": |
55 | | - hue_stmt = f", hue='{hue}'{color_stmt}" if hue else "" |
| 69 | + case "Heatmap": |
| 70 | + method = params.get("Correlation Method", "pearson") |
| 71 | + annot = f", annot=True" if params.get("Annotate Values", True) else "" |
56 | 72 | code += ( |
57 | | - f"plt.figure(figsize=(8, 6))\n" |
58 | | - f"sns.scatterplot(data={df}, x='{x}', y='{y}'{hue_stmt})\n" |
59 | | - f"{title_stmt}" |
60 | | - f"{rot_stmt}" |
61 | | - f"plt.show()\n" |
| 73 | + f"correlation_matrix = {df}[{df}.select_dtypes(include='number').columns].corr(method='{method}')\n" |
| 74 | + f"sns.heatmap(correlation_matrix{annot}{cmap}, fmt='.2f', cbar=True)\n" |
62 | 75 | ) |
63 | 76 |
|
64 | | - case "Line Plot": |
65 | | - hue_stmt = f", hue='{hue}'{color_stmt}" if hue else "" |
| 77 | + case "Pair Plot": |
| 78 | + hue_stmt = f", hue='{params['Hue']}'" if params.get("Hue") else "" |
| 79 | + kind_stmt = f", kind='{params['Kind']}'" if params.get("Kind") else "" |
66 | 80 | code += ( |
67 | | - f"plt.figure(figsize=(8, 6))\n" |
68 | | - f"sns.lineplot(data={df}, x='{x}', y='{y}'{hue_stmt})\n" |
69 | | - f"{title_stmt}" |
70 | | - f"{rot_stmt}" |
71 | | - f"plt.show()\n" |
| 81 | + f"sns.pairplot({df}.select_dtypes(include='number'){hue_stmt}{palette}{kind_stmt})\n" |
72 | 82 | ) |
73 | 83 |
|
| 84 | + code += "plt.show()" |
| 85 | + |
74 | 86 | if withImport: |
75 | 87 | imports_code = "\n".join(import_stmts) |
76 | 88 | code = f"{imports_code}\n\n{code}" |
|
0 commit comments