2016-07-12 70 views
-1

我有一个pyplot代码。
因为我想分组多个酒吧,我想用plt.annotate在图中写文本。
但是,正如你在图片中看到的,左下角的'Something'这个词会被裁剪掉。有谁知道我该如何解决这个问题?
这里是我的代码如何设置更多边距

#!/usr/bin/python 
import matplotlib 
matplotlib.use('Agg') 
import matplotlib.pyplot as plt 
import matplotlib.cm as cm 
import operator as o 
import numpy as np 

n_groups = 5 
means_men = (20, 35, 30, 35, 27) 
std_men = (2, 3, 4, 1, 2) 

means_women = (25, 32, 34, 20, 25) 
std_women = (3, 5, 2, 3, 3) 
fig, ax = plt.subplots() 
index = np.arange(n_groups) 
bar_width = 0.35 

opacity = 0.4 
error_config = {'ecolor': '0.3'} 
rects1 = plt.bar(index, means_men, bar_width, alpha=opacity, color='b', yerr=std_men, error_kw=error_config, label='Men') 

rects2 = plt.bar(index + bar_width, means_women, bar_width, 
       alpha=opacity, 
       color='r', 
       yerr=std_women, 
       error_kw=error_config, 
       label='Women') 

#plt.xlabel('Group') 
plt.ylabel('Scores') 
plt.title('Scores by group and gender') 
plt.annotate('Something', (0,0), (50,-40), xycoords = 'axes fraction', textcoords='offset points', va='top'); 
plt.annotate('Something', (0,0), (200,-20), xycoords = 'axes fraction', textcoords='offset points', va='top'); 
plt.xticks(index + bar_width, ('A', 'B', 'C', 'D', 'E')) 
plt.legend() 

plt.savefig('barchart_3.png') 

enter image description here

回答

0

出于某种原因,有时matplotlib剪辑太积极。如果添加bbox_inches='tight'保存此图应包括数字正确,

plt.savefig('barchart_3.png', bbox_inches='tight') 

更一般地,你可以调整你的东西,如主图,

plt.subplots_adjust(bottom=0.1) 
+0

谢谢!这有帮助! :) –