【问题标题】:Python Pylab, how to alter the size of the label specifying the magnitude of the axesPython Pylab,如何更改指定轴大小的标签大小
【发布时间】:2014-06-17 01:17:36
【问题描述】:

我正在尝试绘制核衰变的微分横截面,因此 y 轴的大小在 10^-38 (m^2) pylab 附近,因为默认将轴绘制为 0.0,0.2,0.4... 等,并且在顶部有一个 '1e-38' y 轴。

我需要增加一点点的字体大小,我已经尝试调整标签大小

py.tick_params(axis='y', labelsize=20)

但这只会调整标签0.0,0.2,0.4....

非常感谢大家的帮助

【问题讨论】:

    标签: python matplotlib label axis magnitude


    【解决方案1】:

    您可以使用ax.yaxis.get_offset_text() 访问文本对象。

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Generate some data
    N = 10
    x = np.arange(N)
    y = np.array([i*(10**-38) for i in x])
    
    fig, ax = plt.subplots()
    
    # Plot the data
    ax.plot(x,y)
    
    # Get the text object
    text = ax.yaxis.get_offset_text()
    
    # Set the size.
    text.set_size(30) # Overkill!
    
    plt.show()
    

    我已经使用matplotlib.pyplot 而不是pylab 编写了上面的解决方案,但如果您绝对必须使用pylab,那么它可以更改(尽管我建议您使用@ 987654329@ 在任何情况下都是pretty much identical,您可以更轻松地使用pyplot 做更多事情。

    编辑

    如果您要使用pylab,那么代码将是:

    pylab.plot(x, y)
    
    ax = pylab.gca() # Gets the current axis object
    
    text = ax.yaxis.get_offset_text() # Get the text object
    
    text.set_size(30) # # Set the size.
    
    pylab.show()
    

    带有(矫枉过正!)偏移文本的示例图。

    【讨论】:

    • 太棒了,谢谢,我以前从未使用过 pyplot,但我与 pylab 一起使用的所有其他命令似乎都运行良好!感谢您如此快速而彻底的回答!
    • 如果你确实想使用pylab,那么你可以使用命令pylab.gca().yaxis.get_offset_text().set_size(30),它的作用与我上面输入的所有内容相同(但看起来有点混乱:))
    猜你喜欢
    • 2011-07-18
    • 1970-01-01
    • 2022-09-28
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多