我有以下数据集.我需要绘制表示1,2或全部的barchat.当我绘制单个数据项的图表时(例如:xdata = [0]和ydata = [1000],xlabels = [‘ first’]时,条码占据整个绘图区域.如何限制条码宽度为0.45?
ydata=[1000,250,3000,500,3200,4000,2000]
xlabels=['first','sec','third','fourth','fifth','sixth','seventh']
barwidth = 0.45
import matplotlib.pyplot as plt
def create_bar_plot(entries):
assert entries > 0
xdata = range(entries)
xlabels=xlabels[:entries]
xdata=xdata[:entries]
ydata=ydata[:entries]
figure = plt.figure(figsize = (12,6), facecolor = "white")
ax = figure.add_subplot(1,1,1)
plt.grid(True)
if xdata and ydata:
ax.bar(xdata, ydata, width=barwidth,align='center',color='blue')
ax.set_xlabel('categories',color='black')
ax.set_ylabel('duration in minutes',color='black')
ax.set_title('duration plot created ')
ax.set_xticks(xdata)
ax.set_xticklabels(xlabels)
figure.autofmt_xdate(rotation=30)
plt.show()
当我尝试
create_bar_plot(5)
我得到这个数字
但是当我打电话
create_bar_plot(1)
我懂了
那么,如何使图以固定的宽度显示每个条形图?似乎bar()中的width = barwidth不能按我预期的那样工作.很可能我丢失了一些东西.
请帮忙