【问题标题】:How to add a clear border around a graph with matplotlib.pyplot如何使用 matplotlib.pyplot 在图形周围添加清晰的边框
【发布时间】:2020-02-21 00:21:04
【问题描述】:

我使用 matplotlib.pyplot 创建了一个堆叠条形图,但图形周围没有边框,因此图形和轴的标题正好位于图像的边缘,并且在我使用它时在某些情况下会被截断。我想在图形、轴和标题周围添加一个小的清晰或白色边框。 repos_amount 是一个熊猫数据框。

这是我的代码:

colors = ["Green", "Red","Blue"]
repos_amount[['Agency','MBS','Treasury']].plot.bar(stacked=True, color=colors, figsize=(15,7))
plt.title('Total Outstanding Fed Repos Operations', fontsize=16)
plt.ylabel('$ Billions', fontsize=12)

图表如下所示:

我尝试了以下链接中的建议,但不知道如何使其发挥作用。我对 matplotlib 还不是很好,所以我需要帮助弄清楚如何将它应用到我的代码中。 How to draw a frame on a matplotlib figure

【问题讨论】:

标签: python pandas matplotlib


【解决方案1】:

尝试在代码底部添加plt.tight_layout()

文档表明这会尝试将标题、标签等置于子图图形大小内,而不是在该图形大小周围添加项目。

如果您的标签或标题太大,可能会产生不良结果,在这种情况下,您需要查看 this 线程中的答案以调整图表元素的特定框大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多