【问题标题】:Use scientific notaion in Xaxis python在 X Axis python 中使用科学记数法
【发布时间】:2021-04-14 11:03:15
【问题描述】:

我正在尝试在 xaxis 上绘制具有一些非常小的值的数据集。我想对 Xaxis 上的所有数字使用科学记数法。所以我尝试了

plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))

我收到以下错误:

xis.major.formatter.set_scientific(is_sci_style)
AttributeError: 'FuncFormatter' object has no attribute 'set_scientific'

谁能帮帮我,非常感谢。

我的情节:

【问题讨论】:

    标签: python matplotlib plot scientific-notation


    【解决方案1】:

    要应用科学记数法,您可以使用格式化程序功能。见这篇文章:Can I turn of scientific notation in matplotlib bar chart?。我在引用的参考文献中应用了与用户相同的逻辑,以获得以下简单示例,即两个轴上都带有科学计数法的正弦函数图。

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.ticker import FuncFormatter
    
    x = np.linspace(0,100,1000)
    y = np.sin(x)
    
    def scientific(x, pos):
        # x:  tick value
        # pos: tick position
        return '%.2E' % x
    
    # create figure
    plt.figure()
    # plot sine
    plt.plot(x,y)
    # get current axes 
    ax = plt.gca()
    # initialize formatter
    scientific_formatter = FuncFormatter(scientific)
    # apply formatter on x and y axes
    ax.xaxis.set_major_formatter(scientific_formatter)
    ax.yaxis.set_major_formatter(scientific_formatter)
    # show plot
    plt.show()
    

    【讨论】:

    • 嘿有没有办法使用 plt.而不是斧头?因为我的整个情节都建立在 plt.
    • 您可以使用ax = plt.gca() 获取当前绘图的轴。请参阅代码编辑。
    猜你喜欢
    • 2019-10-24
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    相关资源
    最近更新 更多