pandas 如何在python中为绘图添加填充?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42281851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:00:10  来源:igfitidea点击:

How to add padding to a plot in python?

pythonpython-3.xpandasmatplotlibseaborn

提问by Aizzaac

I am trying to add padding to the left and the right side of my plot. But when I change xlim and ylim; the image becomes smaller.

我试图在我的情节的左侧和右侧添加填充。但是当我改变 xlim 和 ylim 时;图像变小。

what am I doing wrong?

我究竟做错了什么?

import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = False
from matplotlib.font_manager import FontProperties
import seaborn as sns
%matplotlib inline



df1=df['Total Acc'].round(4)*100 
labels = ['AWA','Rem', 'S1', 'S2', 'SWS', 'SX', 'ALL'] 


rows = [df1.loc[label] for label in labels] 
for row in rows:  
    row.plot(figsize=(10, 5), marker='o')


# http://matplotlib.org/api/legend_api.html ---set_bbox_to_anchor(bbox, transform=None)
myLegend=plt.legend(labels, bbox_to_anchor=(0., 1.15, 1., .102), prop ={'size':10}, loc=10, ncol=7,  #left, bottom, width, height
                title=r'LEARNING CURVE - Fp1_RF(20)')                                         
myLegend.get_title().set_fontsize('18') 


plt.ylim(97.5, 98.5)
plt.xlim(0, 45) 

plt.xlabel('# of samples per subject')
plt.ylabel('Accuracy')  

enter image description here

在此处输入图片说明

回答by Spandan Brahmbhatt

If your matplotlib figure is getting trimmed, you can use Tight Layout. Details provided here

如果您的 matplotlib 图被修剪,您可以使用Tight Layout. 此处提供详细信息

In your code, try adding

在您的代码中,尝试添加

plt.tight_layout()

Another option that you can try is to use subplots_adjust(). Basically it provides you control over the default spacing on the left, right, bottom, and top as well as the horizontal and vertical spacing between multiple rows and columns. Sample Example here

您可以尝试的另一个选项是使用subplots_adjust(). 基本上,它可以让您控制左侧、右侧、底部和顶部的默认间距以及多行和多列之间的水平和垂直间距。示例示例在这里

plt.subplots_adjust(left=0.5, right=0.5)