Python和Matplotlib中的垂直直方图

Cup*_*tor 10 python plot numpy matplotlib histogram

如何制作垂直直方图.有什么选择,还是应该从头开始构建?我想要的是上图看起来像下面的图,但在垂直轴上!

from matplotlib import pyplot as plt
import numpy as np
sample=np.random.normal(size=10000)
vert_hist=np.histogram(sample,bins=30)
ax1=plt.subplot(2,1,1)
ax1.plot(vert_hist[0],vert_hist[1][:-1],'*g')

ax2=plt.subplot(2,1,2)
ax2.hist(sample,bins=30)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

mwa*_*kom 23

使用orientation="horizontal"ax.hist:

from matplotlib import pyplot as plt
import numpy as np

sample = np.random.normal(size=10000)

vert_hist = np.histogram(sample, bins=30)
ax1 = plt.subplot(2, 1, 1)
ax1.plot(vert_hist[0], vert_hist[1][:-1], '*g')

ax2 = plt.subplot(2, 1, 2)
ax2.hist(sample, bins=30, orientation="horizontal");
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Vel*_*ker 5

只是barh()用于情节:

import math
from matplotlib import pyplot as plt
import numpy as np
sample=np.random.normal(size=10000)
vert_hist=np.histogram(sample,bins=30)

# Compute height of plot.
height = math.ceil(max(vert_hist[1])) - math.floor(min(vert_hist[1]))

# Compute height of each horizontal bar.
height = height/len(vert_hist[0])

ax1=plt.subplot(2,1,1)
ax1.barh(vert_hist[1][:-1],vert_hist[0], height=height)

ax2=plt.subplot(2,1,2)
ax2.hist(sample,bins=30)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述