Count-Plot
Sat 17 May 2025
title: "Count Plot (Buggy)" author: "Rj" date: 2020-09-05 description: "List Test" type: technical_note draft: false
# https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import warnings; warnings.filterwarnings(action='once')
# Import Data
df = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv")
df_counts = df.groupby(['hwy', 'cty']).size().reset_index(name='counts')
df.head()
| manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | audi | a4 | 1.8 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
| 1 | audi | a4 | 1.8 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
| 2 | audi | a4 | 2.0 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
| 3 | audi | a4 | 2.0 | 2008 | 4 | auto(av) | f | 21 | 30 | p | compact |
| 4 | audi | a4 | 2.8 | 1999 | 6 | auto(l5) | f | 16 | 26 | p | compact |
df_counts
/Users/rajacsp/opt/anaconda3/envs/py38_jupyter/lib/python3.8/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
and should_run_async(code)
| hwy | cty | counts | |
|---|---|---|---|
| 0 | 12 | 9 | 5 |
| 1 | 14 | 11 | 2 |
| 2 | 15 | 11 | 10 |
| 3 | 16 | 11 | 3 |
| 4 | 16 | 12 | 2 |
| ... | ... | ... | ... |
| 73 | 36 | 25 | 1 |
| 74 | 37 | 28 | 1 |
| 75 | 41 | 29 | 1 |
| 76 | 44 | 33 | 1 |
| 77 | 44 | 35 | 1 |
78 rows × 3 columns
# Draw Stripplot
fig, ax = plt.subplots(figsize=(16,10), dpi= 80)
sns.stripplot(df_counts.cty, df_counts.hwy, size=df_counts.counts*2, ax=ax)
# Decorations
plt.title('Counts Plot - Size of circle is bigger as more points overlap', fontsize=22)
plt.show()
/Users/rajacsp/opt/anaconda3/envs/py38_jupyter/lib/python3.8/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
and should_run_async(code)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-74fe28045231> in <module>
1 # Draw Stripplot
2 fig, ax = plt.subplots(figsize=(16,10), dpi= 80)
----> 3 sns.stripplot(df_counts.cty, df_counts.hwy, size=df_counts.counts*2, ax=ax)
4
5 # Decorations
~/opt/anaconda3/envs/py38_jupyter/lib/python3.8/site-packages/seaborn/categorical.py in stripplot(x, y, hue, data, order, hue_order, jitter, dodge, orient, color, palette, size, edgecolor, linewidth, ax, **kwargs)
2796 linewidth=linewidth))
2797
-> 2798 plotter.plot(ax, kwargs)
2799 return ax
2800
~/opt/anaconda3/envs/py38_jupyter/lib/python3.8/site-packages/seaborn/categorical.py in plot(self, ax, kws)
1187 def plot(self, ax, kws):
1188 """Make the plot."""
-> 1189 self.draw_stripplot(ax, kws)
1190 self.add_legend_data(ax)
1191 self.annotate_axes(ax)
~/opt/anaconda3/envs/py38_jupyter/lib/python3.8/site-packages/seaborn/categorical.py in draw_stripplot(self, ax, kws)
1163 kws.update(c=palette[point_colors])
1164 if self.orient == "v":
-> 1165 ax.scatter(cat_pos, strip_data, **kws)
1166 else:
1167 ax.scatter(strip_data, cat_pos, **kws)
~/opt/anaconda3/envs/py38_jupyter/lib/python3.8/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
1436 def inner(ax, *args, data=None, **kwargs):
1437 if data is None:
-> 1438 return func(ax, *map(sanitize_sequence, args), **kwargs)
1439
1440 bound = new_sig.bind(ax, *args, **kwargs)
~/opt/anaconda3/envs/py38_jupyter/lib/python3.8/site-packages/matplotlib/cbook/deprecation.py in wrapper(*inner_args, **inner_kwargs)
409 else deprecation_addendum,
410 **kwargs)
--> 411 return func(*inner_args, **inner_kwargs)
412
413 return wrapper
~/opt/anaconda3/envs/py38_jupyter/lib/python3.8/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, **kwargs)
4446 s = np.ma.ravel(s)
4447 if len(s) not in (1, x.size):
-> 4448 raise ValueError("s must be a scalar, or the same size as x and y")
4449
4450 c, colors, edgecolors = \
ValueError: s must be a scalar, or the same size as x and y

Score: 5
Category: plot