【问题标题】:How to set the font size for labels in pd.DataFrame.plot()? (pandas)如何在 pd.DataFrame.plot() 中设置标签的字体大小? (熊猫)
【发布时间】:2020-09-25 11:33:13
【问题描述】:

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

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

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

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)

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

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

【问题讨论】:

    标签: python pandas dataframe matplotlib


    【解决方案1】:

    正如documentation 所述,结果是一个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 选项根据需要不断更改对象。

    【讨论】:

      猜你喜欢
      • 2019-01-15
      • 1970-01-01
      • 2019-04-03
      • 2017-02-24
      • 1970-01-01
      • 2012-09-08
      • 2023-01-16
      • 1970-01-01
      相关资源
      最近更新 更多