将2D NumPy数组转换为1D数组以绘制直方图 [英] Turn 2D NumPy array into 1D array for plotting a histogram

查看:641
本文介绍了将2D NumPy数组转换为1D数组以绘制直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib绘制直方图. 我需要转换我的单行2D数组

I'm trying to plot a histogram with matplotlib. I need to convert my one-line 2D Array

[[1,2,3,4]] # shape is (1,4)

插入一维数组

[1,2,3,4] # shape is (4,)

我该怎么做?

推荐答案

您可以直接为该列编制索引:

You can directly index the column:

>>> import numpy as np
>>> x2 = np.array([[1,2,3,4]])
>>> x2.shape
(1, 4)
>>> x1 = x2[0,:]
>>> x1
array([1, 2, 3, 4])
>>> x1.shape
(4,)

或者您可以使用挤压:

>>> xs = np.squeeze(x2)
>>> xs
array([1, 2, 3, 4])
>>> xs.shape
(4,)

这篇关于将2D NumPy数组转换为1D数组以绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆