【问题标题】:How to add padding to a plot in python?如何在python中为绘图添加填充?
【发布时间】:2017-07-06 00:44:08
【问题描述】:

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

我做错了什么?

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

【问题讨论】:

    标签: python python-3.x pandas matplotlib seaborn


    【解决方案1】:

    如果你的 matplotlib 图形被修剪,你可以使用Tight Layout。详情提供here

    在您的代码中,尝试添加

    plt.tight_layout()
    

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

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

    【讨论】:

    • 它们都不起作用。曲线从 x=10 开始。我希望轴从 0 开始。这就是我使用的原因:plt.xlim(0, 45)。这是行不通的。同时,如果我为 Y 轴 (plt.ylim(0, 91); 它工作。
    猜你喜欢
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2017-03-20
    • 2015-11-20
    • 2018-07-16
    • 1970-01-01
    相关资源
    最近更新 更多