【问题标题】:how to make the text size of the x and y axis labels and the title on matplotlib and prettyplotlib graphs bigger如何使 x 和 y 轴标签的文本大小以及 matplotlib 和 prettyplotlib 图形上的标题更大
【发布时间】:2015-02-05 15:36:00
【问题描述】:

我将 matplotlib 或 prettyplotlib 图形的图形大小设置为很大。 例如,假设尺寸为 80 高 x 80 宽。

绘图标题、x 和 y 轴标签(即点标签 2014-12-03 和轴标签 [一年中的月份])的文本大小变得非常小,以至于无法阅读。

如何增加这些文本标签的大小?现在我必须用网络浏览器放大才能看到它们。

【问题讨论】:

    标签: python matplotlib pandas prettyplotlib


    【解决方案1】:

    size 属性:

    import matplotlib.pyplot as plt
    plt.xlabel('my x label', size = 20)
    plt.ylabel('my y label', size = 30)
    plt.title('my title', size = 40)
    plt.xticks(size = 50)
    plt.yticks(size = 60)
    

    例子:

    import numpy as np
    import matplotlib.pyplot as plt
    mu, sigma = 100, 15
    x = mu + sigma * np.random.randn(10000)
    n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)
    plt.xlabel('Smarts', size = 20)
    plt.ylabel('Probability')
    plt.title('Histogram of IQ', size = 50)
    plt.text(60, .025, r'$\mu=100,\ \sigma=15$', size = 30)
    plt.axis([40, 160, 0, 0.03])
    plt.xticks(size = 50)
    plt.yticks(size = 50)
    plt.grid(True)
    plt.show()
    

    --------- 编辑 --------------

    使用漂亮的情节

    fig, ax = plt.plot()
    fig.suptitle('fig title', size = 80)
    ax.set_title('my title', size = 10)
    ax.set_xlabel('my x label', size = 20)
    ax.set_ylabel('my y label', size = 30)
    for tick in ax.xaxis.get_major_ticks():
        tick.label.set_fontsize(40) 
    for tick in ax.yaxis.get_major_ticks():
        tick.label.set_fontsize(50) 
    

    --------- 传奇 --------------

    使用prop 属性

    ppl.legend(prop={'size':20})
    plt.legend(prop={'size':20})
    

    同样的命令..


    示例:

    import matplotlib.pyplot as plt
    import matplotlib as mpl
    from prettyplotlib import brewer2mpl
    import numpy as np
    import prettyplotlib as ppl
    np.random.seed(12)
    fig, ax = plt.subplots(1)
    fig.suptitle('fig title', size = 80)
    ax.set_title('axes title', size = 50)
    for tick in ax.xaxis.get_major_ticks():
        tick.label.set_fontsize(60)
    ax.set_ylabel("test y", size = 35)
    for i in range(8):
        y = np.random.normal(size=1000).cumsum()
        x = np.arange(1000)
        ppl.plot(ax, x, y, label=str(i), linewidth=0.75)
    ppl.legend(prop={'size':30})
    fig.savefig('plot_prettyplotlib_default.png')
    

    【讨论】:

    • 感谢这很好用,是否也可以增加图例的大小,并且 prettyplot lib 是否有另一种语法来指定标题、xlabel、ylabel 以及 x 和 y 刻度的大小?当我将 plt 更改为 .ppt (对于 prettyplot lib)它失败了。
    • 检查答案编辑。对于 plt 和 ppl,图例将以相同的方式起作用。
    • 谢谢@Joao Paulo - 有没有办法在 df.plot(title=MyTitle) 中指定标题的大小。如果我将 plt.title(MyTitle, size=20) 移到 PLOT 括号之外,那么在我的 for 循环中它会在每个子图上方创建一个标题,我不希望这样。我想要一组子图的单个标题 - 但我不知道如何在括号中应用 size 属性 plot(title=MyTitle)。
    • 可以设置图的标题,使用fig对象:
    • fig.suptitle('axes title', size = 80)
    猜你喜欢
    • 2022-11-10
    • 2021-01-03
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 2012-02-05
    • 2020-09-14
    • 2014-09-11
    相关资源
    最近更新 更多