Home > database >  How to display only the relevant values of x on a logarithmic scale?
How to display only the relevant values of x on a logarithmic scale?

Time:09-28

I would like the logarithmic scale to display only the relevant x values in decimal and not powers of 10.

Do you have any idea how I can do this?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x = sorted([0.09, 20, 0.6, 6, 0.05, 0.5, 0.4, 4, 0.3, 3, 2, 0.1, 0.2,
            0.9, 0.8, 10, 0.7, 1, 0.08, 0.07, 0.06, 0.04, 0.03, 0.02, 0.01, 0.005])
y = list(np.arange(0.0, 2.6, 0.1))

data = {'x': x,
        'y': y}

df = pd.DataFrame(data)
df.plot(x='x')
plt.ylim(0, max(y) 0.5)
plt.xscale('log', base=10)
plt.grid()
plt.show()

enter image description here

CodePudding user response:

Use ScalerFormatter:

from matplotlib.ticker import ScalarFormatter
ax = plt.gca()
ax.xaxis.set_major_formatter(ScalarFormatter())
ax.set_xticks(x)

ScalarFormatter

  • Related