Dem*_*nos 4 python matplotlib histogram
我想绘制几个直方图,类似于绘制这些条形图的方式.我已经尝试使用返回的数组hist,但似乎返回了bin边缘,所以我无法使用它们bar.
有没有人有什么建议?
如果您使用np.histogram预先计算直方图,则会发现您将获得hist数组和bin 边缘.plt.bar期望bin中心,所以计算它们:
xs = (bins[:-1] + bins[1:])/2
Run Code Online (Sandbox Code Playgroud)
要适应Matplotlib示例:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
nbins = 50
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
ys = np.random.normal(loc=10, scale=10, size=2000)
hist, bins = np.histogram(ys, bins=nbins)
xs = (bins[:-1] + bins[1:])/2
ax.bar(xs, hist, zs=z, zdir='y', color=c, ec=c, alpha=0.8)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3088 次 |
| 最近记录: |