【问题标题】:How to make the x-axis scale as a^3?如何使x轴刻度为a^3?
【发布时间】:2020-10-08 23:00:33
【问题描述】:

我想画一个像这样的图:

import matplotlib.pyplot as plt    

x = [1**3, 2**3, 3**3, 4**3, 5**3] # i.e., [1,8,27,64,125]
y = [1,2,3,4,5]
plt.plot(x,y)
plt.show()

我想让 x 轴缩放看起来像“对数”缩放。但不是 10^n,刻度标签应该位于 a^3 (a=1, 2, 3, 4, 5)。如果你能分享一些想法,我很感激。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您可以为 x 轴设置对数刻度,并明确设置刻度和刻度标签:

    import matplotlib.pyplot as plt
    from matplotlib.ticker import NullLocator
    
    x = [1**3, 2**3, 3**3, 4**3, 5**3] # i.e., [1,8,27,64,125]
    y = [1, 2, 3, 4, 5]
    plt.plot(x, y)
    plt.xscale('log')
    plt.xticks(x, [f'${i}^3$' for i in range(1, 6)])
    plt.gca().xaxis.set_minor_locator(NullLocator()) # remove the minor ticks created by plt.xscale('log')
    
    plt.show()
    

    【讨论】:

    猜你喜欢
    • 2020-05-29
    • 1970-01-01
    • 2012-12-12
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多