Python画频率直方图以及三维柱状图,cdf图像

您所在的位置:网站首页 用python做函数图像 Python画频率直方图以及三维柱状图,cdf图像

Python画频率直方图以及三维柱状图,cdf图像

2023-07-08 22:18| 来源: 网络整理| 查看: 265

在matlab中,一般是这么做的,如下代码所示。

[y,x]=hist(area_Fluctuation,bins);%频数和中心 yy=y./sum(y);%频率 f3=bar(x,yy,1);

这样可以通过bar函数绘制频率直方图,但是在Python中,对应代码如下:

def my_histogram(x, bin_centers): a = np.min(x) if np.min(x) < bin_centers[0] else bin_centers[0] - 0.05 b = np.max(x) if np.max(x) > bin_centers[-1] else bin_centers[-1] + 0.05 bin_edges = np.r_[a, 0.5 * (bin_centers[:-1] + bin_centers[1:]), b] counts, _ = np.histogram(x, bin_edges) edge = bin_centers return counts, edge, bin_edge y, x, xx = my_histogram(area_Fluctuation, np.arange(0.05, 1, 0.05)) # 修改,持续时间 yy = y / np.sum(y) plt.bar(x,yy)

首先是代码中的my_histogram是可以算出每个组的频数的,然后他这个histogram和matlab中的hist不一样,好像是分组上有区别,不能直接对照着用,按我这么写就一致的,其中bin_centers对应的就是matlab的分组。

其次,由于横坐标如果是小数,那么画的图会非常错误,但是查了半天感觉不好整,于是如下方式画频率直方图:

y, x, xx = my_histogram(area_Fluctuation, bins) weights = np.ones_like(area_Fluctuation) / float(np.size(area_Fluctuation)) plt.hist(area_Fluctuation, xx, weights=weights)

这个代码中,如果你是直接使用的plt.hist中的densit=True那么会发现他算不是频率,但是按我这么写加个权重,那么最终算出来的就是频率了。

以下是cdf图像的绘制代码:

sort_data = np.sort(area_output_level_samping.ravel()) cdf = np.arange(1, np.size(sort_data) + 1) / np.size(sort_data) plt.plot(sort_data, cdf, color='r', linewidth=2) # cdfplot函数绘制累计分布图

以下是三维柱状图绘制代码:

fig = plt.figure() ax = Axes3D(fig) fig.add_axes(ax) xx, yy = np.meshgrid(np.arange(1, 12), np.arange(1, 12)) # 网格化坐标 X, Y = xx.ravel(), yy.ravel() # 矩阵扁平化 bottom = np.zeros_like(X) # 设置柱状图的底端位值 P = P.T.ravel() # 扁平化矩阵 width = height = 1 # 每一个柱子的长和宽 color_map = plt.cm.get_cmap('jet') #设置colormap ax.bar3d(X, Y, bottom, width, height, P, color=color_map(P),edgecolor="black", shade=False,alpha = 0.7) # plt.show()



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3