正在从绘图中剪切的文本-Matplotlib/Python
我用下面的代码绘制了一个简单的方框图,但是从结果中可以看出,一些单词是从图像中剪切出来的。我怎样才能解决这个问题正在从绘图中剪切的文本-Matplotlib/Python,python,matplotlib,Python,Matplotlib,我用下面的代码绘制了一个简单的方框图,但是从结果中可以看出,一些单词是从图像中剪切出来的。我怎样才能解决这个问题 def generate_data_boxplots(data, ticks, x_axis_label, y_axis_label, file_path): """ Plot multiple data boxplots in parallel :param data : a set of data to be plotted :param x_ax
def generate_data_boxplots(data, ticks, x_axis_label, y_axis_label, file_path):
"""
Plot multiple data boxplots in parallel
:param data : a set of data to be plotted
:param x_axis_label : the x axis label of the data
:param y_axis_label : the y axis label of the data
:param file_path : the path where the output will be save
"""
plt.figure()
bp = plt.boxplot(data, sym='r+')
plt.xticks(numpy.arange(1, len(ticks)+1), ticks, rotation=15)
plt.xlabel(x_axis_label)
plt.ylabel(y_axis_label)
# Overplot the sample averages, with horizontal alignment in the center of each box
for i in range(len(data)):
med = bp['medians'][i]
plt.plot([numpy.average(med.get_xdata())], [numpy.average(data[i])], color='w', marker='s',
markeredgecolor='k')
plt.savefig(file_path + '.png')
plt.close()
您可以使用它来帮助减少文本被截断时出现的问题
plt.tight_layout()
将调整子地块参数,以确保所有对象都位于正确的区域内
生成绘图时,只需在
plt.show()
之前调用plt.tight\u layout()
。使用fig.tight\u layout
或将一些附加参数传递给savefig
调用
def generate_data_boxplots(data, ticks, x_axis_label, y_axis_label, file_path):
fig, ax = plt.subplots()
bp = ax.boxplot(data, sym='r+')
plt.xticks(numpy.arange(1, len(ticks)+1), ticks, rotation=15)
ax.set_xlabel(x_axis_label)
ax.set_ylabel(y_axis_label)
# Overplot the sample averages, with horizontal alignment in the center of each box
for i in range(len(data)):
med = bp['medians'][i]
ax.plot([numpy.average(med.get_xdata())], [numpy.average(data[i])], color='w', marker='s',
markeredgecolor='k')
fig.tight_layout() # <----- this
fig.savefig(file_path + '.png')
fig.close()
def生成数据框图(数据、记号、x轴标签、y轴标签、文件路径):
图,ax=plt.子批次()
bp=ax.boxplot(数据,sym='r+'))
plt.xticks(numpy.arange(1,len(ticks)+1),ticks,旋转=15)
ax.set\u xlabel(x\u轴\u标签)
轴设置标签(y轴标签)
#过度抽签样本平均值,在每个框的中心进行水平对齐
对于范围内的i(len(数据)):
med=bp[“中间值”][i]
ax.plot([numpy.average(med.get_xdata())],[numpy.average(data[i]),color='w',marker='s',
markeredgecolor='k')
图1紧_布局图()#
def generate_data_boxplots(data, ticks, x_axis_label, y_axis_label, file_path):
fig, ax = plt.subplots()
bp = ax.boxplot(data, sym='r+')
plt.xticks(numpy.arange(1, len(ticks)+1), ticks, rotation=15)
ax.set_xlabel(x_axis_label)
ax.set_ylabel(y_axis_label)
# Overplot the sample averages, with horizontal alignment in the center of each box
for i in range(len(data)):
med = bp['medians'][i]
ax.plot([numpy.average(med.get_xdata())], [numpy.average(data[i])], color='w', marker='s',
markeredgecolor='k')
fig.savefig(file_path + '.png', bbox_inches='tight') # <------ this
fig.close()