Matplotlib:如何让条形图填满整个 x 轴?
Matplotlib: How do I make my bar plot fill the entire x-axis?
我在 matplotlib 中绘制了一个条形图:
x 刻度没有跨越 x 轴的整个范围。我该如何做到这一点?
我的代码在这里:
def counter_proportions(counter):
total = sum(counter.values())
proportions = dict()
for key, value in counter.items():
proportions[key] = float(value)/float(total)
return proportions
def categorical_counter_xlabels(counter):
idxs = dict()
for i, key in enumerate(counter.keys()):
idxs[key] = i
return idxs
# Use this dummy data
detailed_hosts = ['Species1' * 3, 'Species2' * 1000, 'Species3' * 20, 'Species4' * 20]
# Create a detailed version of the counter, which includes the exact species represented.
detailed_hosts = []
counts = Counter(detailed_hosts)
props = counter_proportions(counts)
xpos = categorical_counter_xlabels(counts)
fig = plt.figure(figsize=(16,10))
ax = fig.add_subplot(111)
plt.bar(xpos.values(), props.values(), align='center')
plt.xticks(xpos.values(), xpos.keys(), rotation=90)
plt.xlabel('Host Species')
plt.ylabel('Proportion')
plt.title("Proportion of Reassortant Viruses' Host Species")
plt.savefig('Proportion of Reassortant Viruses Host Species.pdf', bbox_inches='tight')
手动栏间距
您可以手动控制条形图的位置(例如它们之间的间距),您是这样做的,但使用的是字典 - 而不是尝试使用整数列表来实现。
Import scipy
xticks_pos = scipy.arange( len( counts.keys() )) +1
plt.bar( xticks_pos, props.values(), align='center')
如果您缺少 scipy 并且懒得安装它,这就是 arange() 产生的结果:
In [5]: xticks_pos
Out[5]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
控制边距
上面处理了条之间的间距,正如@JoeKington 在评论中提到的您可以控制的其他部分(例如,如果您不想控制间距而是想限制边距等):
plt.axis('tight')
plt.margins(0.05, 0)
plt.xlim(x.min() - width, x.max() + width))
我在 matplotlib 中绘制了一个条形图:
x 刻度没有跨越 x 轴的整个范围。我该如何做到这一点?
我的代码在这里:
def counter_proportions(counter):
total = sum(counter.values())
proportions = dict()
for key, value in counter.items():
proportions[key] = float(value)/float(total)
return proportions
def categorical_counter_xlabels(counter):
idxs = dict()
for i, key in enumerate(counter.keys()):
idxs[key] = i
return idxs
# Use this dummy data
detailed_hosts = ['Species1' * 3, 'Species2' * 1000, 'Species3' * 20, 'Species4' * 20]
# Create a detailed version of the counter, which includes the exact species represented.
detailed_hosts = []
counts = Counter(detailed_hosts)
props = counter_proportions(counts)
xpos = categorical_counter_xlabels(counts)
fig = plt.figure(figsize=(16,10))
ax = fig.add_subplot(111)
plt.bar(xpos.values(), props.values(), align='center')
plt.xticks(xpos.values(), xpos.keys(), rotation=90)
plt.xlabel('Host Species')
plt.ylabel('Proportion')
plt.title("Proportion of Reassortant Viruses' Host Species")
plt.savefig('Proportion of Reassortant Viruses Host Species.pdf', bbox_inches='tight')
手动栏间距
您可以手动控制条形图的位置(例如它们之间的间距),您是这样做的,但使用的是字典 - 而不是尝试使用整数列表来实现。
Import scipy
xticks_pos = scipy.arange( len( counts.keys() )) +1
plt.bar( xticks_pos, props.values(), align='center')
如果您缺少 scipy 并且懒得安装它,这就是 arange() 产生的结果:
In [5]: xticks_pos
Out[5]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
控制边距
上面处理了条之间的间距,正如@JoeKington 在评论中提到的您可以控制的其他部分(例如,如果您不想控制间距而是想限制边距等):
plt.axis('tight')
plt.margins(0.05, 0)
plt.xlim(x.min() - width, x.max() + width))