【发布时间】:2021-12-30 20:31:14
【问题描述】:
我正在将电子表格从 excel 上传到数据框。在这张表中,我只对两列感兴趣。第一列是日期和时间,格式为 %Y-%m-%d %H-%M-%S。第二列是分类变量,即违规类型(例如迟到)。 总共有几种违规行为。大约6-7种。 使用命令 df.info () 您可以确保可用列的数据框具有日期和时间列的 datatime64[ns] 类型和 >category 类型用于包含违规类型的列。 我想使用 seaborn 库(https://seaborn.pydata.org/examples/hexbin_marginals.html)中的边际分布的 hexbin 图。但是,上面链接中提供的简单代码对于具有类别和时间的变量来说并不是那么简单。
import seaborn as sns
sns.set_theme(style="ticks")
sns.jointplot(x=df['incident'], y=['date-time'], kind="hex", color="#4CB391")
编译器报告 TypeError: The x variable is categorical, but one of ['numeric', 'datetime'] is required
我了解纵坐标轴需要数字变量或日期时间变量。转换并不能解决问题。
这个错误可以用
重现import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
ndf = pd.DataFrame({'date-time': ['2021-11-15 00:10:00','2021-11-15 00:20:00'], 'incident': ['a','b']})
print(ndf)
sns.set_theme(style="ticks")
sns.jointplot(data=ndf, x='incident', y='date-time', color="#4CB391", hue=ndf['incident'] )
plt.show()
问题。如何得到一个情节看起来像seabron style
【问题讨论】:
-
这种图对于纯分类数据没有多大意义。如果有一些从类别到数字的“自然”映射,您可以尝试类似
df['incident']=df['incident'].map({'a': 1, 'b': 3, 'c':2,...})。