简体   繁体   中英

Matplotlib plot already binned data

I want to plot the mean local binary patterns histograms of a set of images. Here is what I did:

#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))    

After that I simply sum up all the LBP histograms and take the mean of them. These are the values found, which are saved in a txt file:

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

I am trying to simply plot these values (don't need to calculate the histogram, because the values are already from a histogram). Here is what I've tried:

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') 

However, look at the Figure these commands produce:

在此处输入图片说明

I still dont understand what is happening. I would like to make a Figure like the one I produced in Excel using the same data, as follows: 在此处输入图片说明

I must confess that I am quite rookie with matplotlib. So, what was my mistake?

Try this. Here the array is your mean values from bins.

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)

The plot look like this. 图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM