我有一个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')
答案 0 :(得分:0)
出于某种原因,matplotlib有时会过于激进地剪辑。如果您添加bbox_inches='tight'
以保存图,则应正确包含该图,
plt.savefig('barchart_3.png', bbox_inches='tight')
更一般地说,您可以使用类似
之类的内容调整主要数字plt.subplots_adjust(bottom=0.1)