如何在条形图上正确放置条形图

``````def makebarplot(bins, values, title = None, export = False,
path = None, file_name = None):
'''
This function will take the outputs
of np.histogram results and make a
barplot of it.
'''
#creating the object
img, ax1 = plt.subplots(figsize = (13,8))

ax1.bar(bins[:-1],values)

ax1.set_xscale('log')
ax1.set_yscale('log')
ax1.set_xlabel('Values')
ax1.set_xticks(bins)
ax1.set_ylabel('Frequency')
ax1.set_title(title)

plt.show()

if export is True:
#saving figure
img.savefig(path+file_name, bbox_inches = 'tight')

#closing the plot
#plt.close()

return

#the bins I'm using
bins = [1e1**i for i in range(2,20)]

#the values I'm entering they are the frequency of
# values in between each of the corresponding
#bins defined above
values = [0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00,
1.000000e+00, 1.000000e+01, 1.100000e+02, 8.870000e+02,
6.582000e+03, 4.542300e+04, 2.630240e+05, 1.090866e+06,
2.281769e+06, 1.199336e+06, 4.057230e+05,
6.878300e+04, 1.211000e+03]

makebarplot(bins,ar_values)
``````

Also I can't understand why I had to trim one of the bins so the values and bins should have the same length. Since the results would fit in between then naturally I would assume that the bins have one more length than the list containing the values.

ImportanceOfBeingErnest

Only because you name the positions of the bars `bins`, does not mean they become bins. (As in "I named my car 'Ferrari', but it didn't become red.")

``````ax1.bar(bins[:-1],values, width=np.diff(bins), align="edge", ec="k")
``````

（这里`np``numpy`

0 条评论