-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter.py
More file actions
339 lines (294 loc) · 14.6 KB
/
Copy pathplotter.py
File metadata and controls
339 lines (294 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from scipy import stats
import pandas as pd
pd.set_option('display.float_format', '{:.2f}'.format)
class Plotter:
def __init__(self, df):
self.df = df
def plot_bar(self, x, y, title, xlabel, ylabel):
# Group by 'x' column, calculate count, and reset index to keep 'x' as a column
data = self.df.groupby(x)[y].count().reset_index()
plt.figure(figsize=(10, 6))
# Use seaborn or matplotlib to plot, ensuring 'x' is used explicitly for the x-axis
sns.barplot(data=data, x=x, y=y, color='skyblue') # Seaborn automatically handles categorical axes better
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(xlabel, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel(ylabel, fontsize=12, fontfamily='serif', color='darkred')
plt.xticks(rotation=45)
plt.grid(True)
plt.show()
def plot_line(self, x, y, title, xlabel, ylabel):
plt.figure(figsize=(10, 6))
self.df.groupby(x)[y].mean().plot(kind='line', color='skyblue')
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(xlabel, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel(ylabel, fontsize=12, fontfamily='serif', color='darkred')
plt.xticks(rotation=45)
plt.grid(True)
plt.show()
def plot_histogram(self, column, title):
plt.figure(figsize=(10, 6))
sns.histplot(self.df[column], kde=True, color='skyblue')
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(column, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel('Frequency', fontsize=12, fontfamily='serif', color='darkred')
plt.grid(True)
plt.show()
def plot_scatter(self, x, y, title, xlabel, ylabel):
plt.figure(figsize=(10, 6))
plt.scatter(self.df[x], self.df[y], color='skyblue')
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(xlabel, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel(ylabel, fontsize=12, fontfamily='serif', color='darkred')
plt.grid(True)
plt.show()
def plot_bar_vis(self, x, y, title, xlabel, ylabel):
plt.figure(figsize=(10, 6))
sns.barplot(x=self.df[x], y=self.df[y], palette='viridis')
plt.title(title, fontsize=20, fontfamily='serif', color='blue')
plt.xlabel(xlabel, fontsize=16, fontfamily='serif', color='darkred')
plt.ylabel(ylabel, fontsize=16, fontfamily='serif', color='darkred')
plt.xticks(rotation=45)
plt.show()
def plot_boxen(self, x, y, title):
plt.figure(figsize=(12, 8))
sns.boxenplot(x=self.df[x], y=self.df[y])
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(x, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel(y, fontsize=12, fontfamily='serif', color='darkred')
plt.show()
def plot_count(self, column, title, xlabel, ylabel):
plt.figure(figsize=(12, 8))
sns.countplot(x=self.df[column], palette='viridis')
plt.title(title, fontsize=20, fontfamily='serif', color='blue')
plt.xlabel(xlabel, fontsize=16, fontfamily='serif', color='darkred')
plt.ylabel(ylabel, fontsize=16, fontfamily='serif', color='darkred')
plt.xticks(rotation=90)
plt.show()
def plot_reg(self, x, y, title, xlabel, ylabel):
plt.figure(figsize=(12, 6))
sns.regplot(x=self.df[x], y=self.df[y], marker='o', color=".3", line_kws=dict(color="r"))
plt.title(title, fontsize=20, fontfamily='serif', color='blue')
plt.xlabel(xlabel, fontsize=16, fontfamily='serif', color='darkred')
plt.ylabel(ylabel, fontsize=16, fontfamily='serif', color='darkred')
plt.show()
def plot_pie(self, column, title, threshold=0.007):
# Calculate counts and create a new Series for plotting
counts = self.df[column].value_counts(normalize=True)
# Combine small categories into "Other"
small_categories = counts[counts < threshold].sum()
counts = counts[counts >= threshold]
counts['Other'] = small_categories
plt.figure(figsize=(12, 12))
counts.plot.pie(autopct='%1.1f%%', colors=sns.color_palette("pastel"))
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.ylabel('')
plt.show()
def plot_grouped_bar(self, x, y, hue, title):
plt.figure(figsize=(12, 8))
sns.barplot(data=self.df, x=x, y=y, hue=hue)
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(x, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel(y, fontsize=12, fontfamily='serif', color='darkred')
plt.legend(title=hue)
plt.show()
"""
2. Top 10 vehicle makes
plot type - bar chart
interactivity - none
"""
plt.figure(figsize=(10, 6))
self.df['make'].value_counts().head(10).plot(kind='bar')
plt.title('Top 10 Vehicle Makes')
plt.xlabel('Make')
plt.ylabel('Sales Volume')
plt.xticks(rotation=45)
plt.show()
def subplot_story(self):
fig, axs = plt.subplots(2, 2, figsize=(14, 10))
# Bar Plot: Vehicle Sales by Year
self.df.groupby('year')['make'].count().plot(kind='bar', ax=axs[0, 0], color='skyblue')
axs[0, 0].set_title('Vehicle Sales by Year', fontsize=12, fontfamily='serif', color='blue')
axs[0, 0].set_xlabel('Year', fontsize=10, fontfamily='serif', color='darkred')
axs[0, 0].set_ylabel('Sales Volume', fontsize=10, fontfamily='serif', color='darkred')
# Bar Plot: Top 10 Vehicle Makes
self.df['make'].value_counts().head(10).plot(kind='bar', ax=axs[0, 1], color='skyblue')
axs[0, 1].set_title('Top 10 Vehicle Makes', fontsize=12, fontfamily='serif', color='blue')
axs[0, 1].set_xlabel('Make', fontsize=10, fontfamily='serif', color='darkred')
axs[0, 1].set_ylabel('Sales Volume', fontsize=10, fontfamily='serif', color='darkred')
# Line Plot: Average Selling Price by Year
self.df.groupby('year')['sellingprice'].mean().plot(kind='line', ax=axs[1, 0], color='skyblue')
axs[1, 0].set_title('Average Selling Price by Year', fontsize=12, fontfamily='serif', color='blue')
axs[1, 0].set_xlabel('Year', fontsize=10, fontfamily='serif', color='darkred')
axs[1, 0].set_ylabel('Average Price', fontsize=10, fontfamily='serif', color='darkred')
# Scatter Plot: Selling Price vs MMR
axs[1, 1].scatter(self.df['sellingprice'], self.df['mmr'], color='skyblue')
axs[1, 1].set_title('Selling Price vs MMR', fontsize=12, fontfamily='serif', color='blue')
axs[1, 1].set_xlabel('Selling Price', fontsize=10, fontfamily='serif', color='darkred')
axs[1, 1].set_ylabel('MMR', fontsize=10, fontfamily='serif', color='darkred')
plt.tight_layout()
plt.show()
def plot_dist(self, column, title):
plt.figure(figsize=(10, 6))
sns.histplot(self.df[column], kde=True, color='skyblue')
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(column, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel('Density', fontsize=12, fontfamily='serif', color='darkred')
plt.grid(True)
plt.show()
def plot_pair(self, columns=None, title='Pair Plot'):
if columns is not None:
g = sns.pairplot(self.df[columns], plot_kws={'alpha': 0.6, 's': 80, 'edgecolor': 'k'})
else:
g = sns.pairplot(self.df, plot_kws={'alpha': 0.6, 's': 80, 'edgecolor': 'k'})
g.fig.suptitle(title, fontsize=15, fontfamily='serif', color='blue')
g.fig.subplots_adjust(top=0.95, right=0.95)
# Set xlabel and ylabel for each plot
for ax in g.axes.flatten():
ax.set_xlabel(ax.get_xlabel(), fontsize=12, fontfamily='serif', color='darkred')
ax.set_ylabel(ax.get_ylabel(), fontsize=12, fontfamily='serif', color='darkred')
plt.show()
def plot_heatmap(self, title='Correlation Heatmap'):
plt.figure(figsize=(14, 12)) # Increase figure size
features = ['sellingprice', 'mmr', 'odometer', 'condition', 'year']
numeric_df = self.df[features].dropna()
if numeric_df.empty:
print("No numeric data available for heatmap.")
return
corr = numeric_df.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm', cbar=True,
xticklabels=corr.columns, yticklabels=corr.columns)
plt.title(title, fontsize=20, fontfamily='serif', color='blue')
plt.xlabel('Features', fontsize=15, fontfamily='serif', color='darkred')
plt.ylabel('Features', fontsize=15, fontfamily='serif', color='darkred')
plt.xticks(rotation=45, ha='right', fontsize=12)
plt.yticks(rotation=0, fontsize=12)
plt.tight_layout()
plt.show()
def plot_hist_kde(self, column, title=None):
if title is None:
title = f'Histogram with KDE of {column}'
plt.figure(figsize=(10, 6))
sns.histplot(self.df[column], kde=True, color='skyblue')
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(column, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel('Frequency', fontsize=12, fontfamily='serif', color='darkred')
plt.grid(True)
plt.show()
def plot_qq(self, column, title=None):
if title is None:
title = f'QQ Plot of {column}'
plt.figure(figsize=(10, 6))
stats.probplot(self.df[column], dist="norm", plot=plt)
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel('Theoretical Quantiles', fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel('Sample Quantiles', fontsize=12, fontfamily='serif', color='darkred')
plt.grid(True)
plt.show()
def plot_kde(self, column, title=None):
if title is None:
title = f'KDE Plot of {column}'
plt.figure(figsize=(10, 6))
sns.kdeplot(self.df[column], fill=True, alpha=0.6, linewidth=2)
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(column, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel('Density', fontsize=12, fontfamily='serif', color='darkred')
plt.grid(True)
plt.show()
def plot_im_reg(self, x, y, title):
plt.figure(figsize=(10, 6))
sns.regplot(x=self.df[x], y=self.df[y], scatter_kws={'s': 10}, line_kws={'color': 'red'})
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(x, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel(y, fontsize=12, fontfamily='serif', color='darkred')
plt.grid(True)
plt.show()
def plot_box(self, x, y, hue=None, title='Default Title'):
plt.figure(figsize=(10, 6))
if hue:
sns.boxplot(x=self.df[x], y=self.df[y], hue=self.df[hue], palette="pastel")
else:
sns.boxplot(x=self.df[x], y=self.df[y])
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(x, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel(y, fontsize=12, fontfamily='serif', color='darkred')
plt.grid(True)
plt.legend(title=hue) if hue else None
plt.show()
# New plot methods for the missing types
def plot_stacked_bar(self, x, y, title, xlabel, ylabel):
plt.figure(figsize=(10, 6))
self.df.groupby([x, y]).size().unstack().plot(kind='bar', stacked=True)
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(xlabel, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel(ylabel, fontsize=12, fontfamily='serif', color='darkred')
plt.grid(True)
plt.show()
def plot_area(self, x, y, title):
plt.figure(figsize=(10, 6))
self.df.plot.area(x=x, y=y)
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(x, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel(y, fontsize=12, fontfamily='serif', color='darkred')
plt.grid(True)
plt.show()
def plot_violin(self, x, y, title):
plt.figure(figsize=(10, 6))
sns.violinplot(data=self.df, x=x, y=y)
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(x, fontsize=12, fontfamily='serif', color='darkred')
plt.ylabel(y, fontsize=12, fontfamily='serif', color='darkred')
plt.grid(True)
plt.show()
def plot_joint_kde_scatter(self, x, y, title):
g = sns.jointplot(data=self.df, x=x, y=y, kind="kde")
g.plot_joint(plt.scatter, c="w", s=30, linewidth=1, marker="+")
g.ax_joint.collections[0].set_alpha(0)
plt.suptitle(title)
plt.grid(True)
plt.show()
def plot_rug(self, column, title):
plt.figure(figsize=(10, 6))
sns.rugplot(data=self.df, x=column)
plt.title(title, fontsize=15, fontfamily='serif', color='blue')
plt.xlabel(column, fontsize=12, fontfamily='serif', color='darkred')
plt.grid(True)
plt.show()
def plot_3d_scatter(self, x, y, z, title):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(self.df[x], self.df[y], self.df[z])
ax.set_xlabel(x)
ax.set_ylabel(y)
ax.set_zlabel(z)
ax.set_title(title)
plt.show()
def plot_cluster_map(self):
# Select only numeric columns from DataFrame
numeric_df = self.df.select_dtypes(include=[np.number])
if numeric_df.empty:
print("No numeric data available for cluster map.")
return
# Compute the correlation matrix
corr_matrix = numeric_df.corr()
# Generate a cluster map
sns.clustermap(corr_matrix, cmap='coolwarm', annot=True)
plt.show()
def plot_hexbin(self, x, y, title, gridsize=40, cmap='inferno'):
plt.hexbin(self.df[x], self.df[y], gridsize=gridsize, cmap=cmap)
plt.colorbar()
plt.title(title)
plt.show()
def plot_strip(self, x, y, title):
plt.figure(figsize=(12, 8))
# sns.stripplot(data=self.df, x=x, y=y)
sns.stripplot(data=self.df, x=x, y=y, size=4)
plt.title(title)
plt.show()
def plot_swarm(self, x, y, title):
plt.figure(figsize=(10, 6))
sns.swarmplot(data=self.df, x=x, y=y,size=3)
plt.title(title)
plt.show()