Add 1 to each x value, then take the log:
import matplotlib.pyplot as plt import numpy as np import matplotlib.ticker as ticker fig, ax = plt.subplots() x = [0, 10, 100, 1000] y = [100, 20, 10, 50] x = np.asarray(x) + 1 y = np.asarray(y) ax.plot(x, y) ax.set_xscale('log') ax.set_xlim(x.min(), x.max()) ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x-1))) ax.xaxis.set_major_locator(ticker.FixedLocator(x)) plt.show()

Use
ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x-1))) ax.xaxis.set_major_locator(ticker.FixedLocator(x))
to change label labels to values โโother than the x log.
(My initial suggestion was to use plt.xticks(x, x-1) , but that would affect all axes. To highlight the changes for individual axes, I changed all command calls to ax , not plt calls. )
matplotlib deletes points containing NaN , inf or -inf . Since log(0) -inf , the point corresponding to x=0 will be removed from the logarithm graph.
If you increase all x-values โโby 1 since log(1) = 0 , the point corresponding to x=0 will not be built on x=log(1)=0 in the log chart.
The remaining x values โโwill also be shifted by one, but this does not matter for the eye, since log(x+1) very close to log(x) for large x values.
unutbu
source share