gpt4 book ai didi

python - 文本被情节切割 - Matplotlib/Python

转载 作者:太空狗 更新时间:2023-10-30 02:04:11 27 4
gpt4 key购买 nike

我正在使用以下代码绘制一个简单的箱线图,但从结果中可以看出,一些文字被剪切了图像。我该如何解决这个问题?

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()

enter image description here

enter image description here

最佳答案

使用 fig.tight_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 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()

关于python - 文本被情节切割 - Matplotlib/Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23312886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com