早晨

我有一个lage df,想用joinplot绘制我的inc_open_hours_bucketinc_volume

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

我在jointplot函数调用中得到了错误TypeError: can't multiply sequence by non-int of type 'float'.

我需要将这些强制为特定类型才能使用这种类型的图表吗?

# 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) 

推荐答案

我需要将这些强制为特定类型才能使用这种类型的图表吗?

是的,您需要将数据转换为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

Python相关问答推荐

剧作家Python:expect(locator).to_be_visible()vs locator.wait_for()

LAB中的增强数组

将整组数组拆分为最小值与最大值之和的子数组

Python json.转储包含一些UTF-8字符的二元组,要么失败,要么转换它们.我希望编码字符按原样保留

从dict的列中分钟

Pandas—合并数据帧,在公共列上保留非空值,在另一列上保留平均值

为什么NumPy的向量化计算在将向量存储为类属性时较慢?'

如何使用Pandas DataFrame按日期和项目汇总计数作为列标题

在Python中计算连续天数

numpy.unique如何消除重复列?

如何获取Python synsets列表的第一个内容?

处理Gekko的非最优解

如何在GEKKO中使用复共轭物

一个telegram 机器人应该发送一个测验如何做?""

计算机找不到已安装的库'

在Django中重命名我的表后,旧表中的项目不会被移动或删除

如何将返回引用的函数与pybind11绑定?

用来自另一个数据框的列特定标量划分Polars数据框中的每一列,

多索引数据帧到标准索引DF

如何在PYTHON中向单元测试S Side_Effect发送额外参数?