[Python] Matplotlib Engformatter base 2

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am plotting some running time vs. data size. The x axis is logarithmic in base 2, but when I try to use EngFormatter, I get the values in base 10.

    Is it possible to get the values in base 2? Like 32k instead of 33k and 64k instead of 66k.

    Here I provide some sample code and figure:

    sizes = np.array([2**i for i in range(1, 18)])
    times = np.array([2**i*np.random.uniform(0.4, 1.6) for i in range(1, 18)]) # Example times in milliseconds

    fig, ax = plt.subplots(figsize=(10, 6))
    plt.plot(sizes, times, marker='o')
    ax.set_xscale('log', base=2)
    ax.set_xlabel('size')
    ax.set_ylabel('time')
    ax.xaxis.set_major_formatter(mticker.EngFormatter(places=0)) # TODO: base 2


    Resulting figure: [​IMG]

    Continue reading...

Compartilhe esta Página