首页 > 解决方案 > 如何在 pd.DataFrame.plot() 中设置标签的字体大小?(熊猫)

问题描述

我想知道是否可以覆盖使用pd.DataFrame.plot()方法生成的绘图的标签大小。按照文档,我可以使用 kwarg 轻松地为 xticks 和 yticks 做到这fontsize一点:

fontsize int,默认无 xticks 和 yticks 的字体大小。

不幸的是,我没有看到类似的选项会改变 xlabel 和 ylabel 的大小。这是一个可视化问题的片段:

import pandas as pd
df = pd.DataFrame(
    [
        {'date': '2020-09-10', 'value': 10},
        {'date': '2020-09-10', 'value': 12},
        {'date': '2020-09-10', 'value': 13},
    ]
)
df.plot(x='date', y='value', xlabel='This is the date.', ylabel='This is the value.', fontsize=10)

使用 fontsize=10 绘图

df.plot(x='date', y='value', xlabel="This is the date.", ylabel="This is the value.", fontsize=20)

在此处输入图像描述

我可以以类似的方式更改 xlabel 和 ylabel 的大小吗?

标签: pythonpandasdataframematplotlib

解决方案


正如文档所述,结果是一个 matplotlib 对象(默认情况下,除非您更改它)。因此,您可以像更改 matplolib 对象一样更改您喜欢的任何内容:

from matplolib import pyplot as plt
plt.xlabel('This is the date.', fontsize=18)
plt.ylabel('This is the value.', fontsize=16)

您可以使用 matplolib 选项根据需要不断更改对象。


推荐阅读