jointplot generating TypeError: can't multiply sequence by non-int of type 'float'

Question:

Morning,

I have a lage df and want to plot my inc_open_hours_bucket by inc_volume using a joinplot

data = {'inc_open_hours_bucket': ['No Data', '0_15_min', '15_30_min', '30_45_min', '45_60_min',
                                  '1_1.5_hour', '1.5_2_hour', '2_2.5_hour', '2.5_3_hour',
                                  '3_3.5_hour', '3.5_4_hour', '4_4.5_hour', '4.5_5_hour',
                                  '5_5.5_hour', '5.5_6_hour', '> 6_hour'],
        'inc_volume': [153, 99, 50, 46, 53, 73, 50, 44, 37, 34, 29, 23, 13, 15, 18, 417]}

df = pd.DataFrame(dict(data))
print(df)

 inc_open_hours_bucket  inc_volume
0                No Data         153
1               0_15_min          99
2              15_30_min          50
3              30_45_min          46
4              45_60_min          53
5             1_1.5_hour          73
6             1.5_2_hour          50
7             2_2.5_hour          44
8             2.5_3_hour          37
9             3_3.5_hour          34
10            3.5_4_hour          29
11            4_4.5_hour          23
12            4.5_5_hour          13
13            5_5.5_hour          15
14            5.5_6_hour          18
15              > 6_hour         417

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 16 entries, 0 to 15
Data columns (total 2 columns):
inc_open_hours_bucket    16 non-null object
inc_volume               16 non-null int64
dtypes: int64(1), object(1)
memory usage: 336.0+ bytes

I’m getting the error TypeError: can't multiply sequence by non-int of type 'float' on the jointplot function call.
I thought as I have a categorical field and an integer field this would be no issue.

Do I need to force these into specific types to use this type of chart?

# Create jointplot
sns.set(font_scale = 0.5)
ax = sns.jointplot(x='inc_open_hours_bucket',y='inc_volume',data=df,kind='hex')
# Add attributes
plt.title("INC by inc_open_hours_bucket")
plt.show()
# Save image
ax_Figure = ax.get_figure()
# Set to high resolution and save
ax_Figure.savefig('12. INC by inc_open_hours_bucket.png', dpi=300) 
Asked By: Peter Lucas

||

Answers:

Do I need to force these into specific types to use this type of chart?

Yes, you need to convert your data, for instance to pandas categories:

import pandas as pd
import seaborn as sns

data = {'inc_open_hours_bucket': ['No Data','0_15_min',
                                  '15_30_min','30_45_min',
                                  '45_60_min','1_1.5_hour',
                                  '1.5_2_hour','2_2.5_hour',
                                  '2.5_3_hour','3_3.5_hour',
                                  '3.5_4_hour','4_4.5_hour',
                                  '4.5_5_hour','5_5.5_hour',
                                  '5.5_6_hour','> 6_hour'],
        'inc_volume': [153,99,50,46,53,73,50,44,37,34,29,23,13,15,18,417]
        }
df = pd.DataFrame(data)
cat_type = pd.CategoricalDtype(df.inc_open_hours_bucket.values)
df.inc_open_hours_bucket = df.inc_open_hours_bucket.astype(cat_type)

ax = sns.jointplot(x=df.inc_open_hours_bucket.cat.codes, y=df.inc_volume, kind='hex')
ax.ax_joint.set_xticks(df.index, df.inc_open_hours_bucket)
ax.ax_joint.tick_params('x', rotation=90)

enter image description here

Answered By: Stef
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.