我想绘制一组图像的平均局部二进制图案直方图。这是我所做的:
#calculates the lbp
lbp = feature.local_binary_pattern(image, 24, 8, method="uniform")
#Now I calculate the histogram of LBP Patterns
(hist, _) = np.histogram(lbp.ravel(), bins=np.arange(0, 27))
之后,我简单地总结所有LBP直方图并取它们的平均值。这些是找到的值,这些值保存在txt文件中:
2.962000000000000000e + 03
1.476000000000000000e + 03
1.128000000000000000e + 03
1.164000000000000000e + 03
1.282000000000000000e + 03
1.661000000000000000e + 03
2.253000000000000000e + 03
3.378000000000000000e + 03
4.490000000000000000e + 03
5.010000000000000000e + 03
4.337000000000000000e + 03
3.222000000000000000e + 03
2.460000000000000000e + 03
2.495000000000000000e + 03
2.599000000000000000e + 03
2.934000000000000000e + 03
2.526000000000000000e + 03
1.971000000000000000e + 03
1.303000000000000000e + 03
9.900000000000000000e + 02
7.980000000000000000e + 02
8.680000000000000000e + 02
1.119000000000000000e + 03
1.479000000000000000e + 03
4.355000000000000000e + 03
3.112600000000000000e + 04
我试图简单地绘制这些值(不需要计算直方图,因为这些值已经来自直方图)。这是我尝试过的:
import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.pyplot as plt
import plotly.plotly as py
#load data
data=np.loadtxt('original_dataset1.txt')
#convert to float
data=data.astype('float32')
#define number of Bins
n_bins = data.max() + 1
plt.style.use("ggplot")
(fig, ax) = plt.subplots()
fig.suptitle("Local Binary Patterns")
plt.ylabel("Frequency")
plt.xlabel("LBP value")
plt.bar(n_bins, data)
fig.savefig('lbp_histogram.png')
但是,请看这些命令产生的图:
我仍然不明白发生了什么。我想制作一个像我在Excel中使用相同数据制作的图形,如下所示:
我必须承认我是matplotlib的新秀。那么,我的错误是什么?
答案 0 :(得分:2)
尝试一下。这里的数组是您从垃圾箱中获得的平均值。
array = [2962,1476,1128,1164,1282,1661,2253]
fig,ax = plt.subplots(nrows=1, ncols=1,)
ax.bar(np.array(range(len(array)))+1,array,color='orangered')
ax.grid(axis='y')
for i, v in enumerate(array):
ax.text(i+1, v, str(v),color='black',fontweight='bold',
verticalalignment='bottom',horizontalalignment='center')
plt.savefig('savefig.png',dpi=150)