python - 如何使用 Seaborn 绘制字符串类型 Numpy 数组?

标签 python numpy seaborn

我正在尝试绘制以下数组:

array(['2019-08-23 16:24:55.000', '2019-08-23 15:51:07.000',
       '2019-08-23 15:41:30.000', '2019-08-23 15:36:45.000',
       '2019-08-23 13:54:36.000', '2019-08-23 11:32:04.000',
       '2019-08-23 11:15:20.000', '2019-08-23 11:07:46.000',
       '2019-08-23 10:51:20.000', '2019-08-23 10:51:19.000',
       '2019-08-23 10:33:24.000', '2019-08-23 09:50:18.000',
       '2019-08-23 08:21:51.000', '2019-08-22 11:50:55.000',
       '2019-08-22 11:36:52.000', '2019-08-22 11:31:24.000',
       '2019-08-22 09:56:18.000', '2019-08-21 21:42:50.000',
       '2019-08-21 21:11:08.000', '2019-08-21 21:09:18.000',
       '2019-08-21 21:04:52.000', '2019-08-21 20:57:57.000',
       '2019-08-21 20:27:09.000', '2019-08-21 20:06:50.000',
       '2019-08-21 20:01:00.000', '2019-08-21 19:50:41.000',
       '2019-08-21 17:53:00.000', '2019-08-21 17:38:45.000',
       '2019-08-21 16:37:32.000', '2019-08-15 14:04:18.000',
       '2019-08-15 13:42:21.000', '2019-08-15 13:36:25.000',
       '2019-08-15 13:27:50.000', '2019-08-15 13:22:55.000'], dtype='<U23')
sns.distplot(nx)
/opt/conda/lib/python3.6/site-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims)
     73             is_float16_result = True
     74 
---> 75     ret = umr_sum(arr, axis, dtype, out, keepdims)
     76     if isinstance(ret, mu.ndarray):
     77         ret = um.true_divide(

TypeError: cannot perform reduce with flexible type

有没有办法绘制这样的数组?

最佳答案

我不完全确定您要做什么,但我想说您需要将数组转换为代表性整数,然后才能进行绘图。因此先转换为日期时间,然后转换为整数:

sns.distplot(nx.astype('datetime64[s]').astype('int'))

enter image description here

现在,如果您想让日期回到 x 轴,也许可以这样做:

g = sns.distplot(nx.astype('datetime64[s]').astype('int'))
g.set_xticklabels(ax.get_xticks().astype('datetime64[s]').astype('datetime64[D]'), rotation=20)

enter image description here

关于python - 如何使用 Seaborn 绘制字符串类型 Numpy 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57626773/

相关文章:

python - 如何用python写numpy : b = sum(v) - a as an implicitly elementwise (vector) computation?

python - 复制/ reshape 高维 numpy 数组

python - 保存的 `matplotlib`/`seaborn` 图中的左右对齐文本

python - 散点图矩阵,输入与输出

python - 意外的 Python 类变量

python - 如何编写一个从列表中递归删除字符实例的函数?

python - 使用 NumPy 计算向量中的值

python - 如何使用 Seaborn 创建 FacetGrid 堆叠条形图?

python - 函数定义上的正则表达式 findall。想要匹配参数但不匹配函数

python - 将 Mysql 时间戳转换为 Python 的日期时间