Plotting x-axis in log scale spacing but not labeling it in exponential form

Question:

I would like to plot say two values x = [0, 10,20,50,100] and y=[1,2,3,10,100] using pylab. I want to keep the spacing of x-axis in log form. But I want to tick at the values of x i’e at 10, 20, 50, 100 and print them as it not in the form of 10e1 or 10e2. I am doing it as follows:

 import matplotlib.pylab as plt
 plt.xscale('log')
 plt.plot(x, y)
 plt.xticks(x)
 plt.grid()

But it keeps the values in the form of 10e1, 10e2.

Could you please help me out?

Asked By: thetna

||

Answers:

I think what you want is to change the major_formatter of the x axis?

import matplotlib.pylab as plt
import numpy as np
from matplotlib.ticker import ScalarFormatter

x = [0, 10,20,50,100]
y=[1,2,3,10,100]

plt.plot(x, y)
plt.xscale('log')
plt.grid()

ax = plt.gca()
ax.set_xticks(x[1:]) # note that with a log axis, you can't have x = 0 so that value isn't plotted.
ax.xaxis.set_major_formatter(ScalarFormatter())

plt.show()

scalar_formatter

Answered By: areuexperienced

The following

import matplotlib.pyplot as plt

x = [0,10,20,50,100]
y = [1,2,3,10,100]

f,ax = plt.subplots()
ax.plot(x,y)
ax.set_xscale('log')
ax.set_xticks(x)
ax.set_xticklabels(x)
ax.set_xlim([0,100])

will produce

enter image description here

Answered By: Zach Fox
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.