1

I am trying to plot an xy-graph whose x-axis is logarithmic. I did that using this command:

import matplotlib as plt
plt.plot(X,Y)
plt.xscale('log')

The x-axis is logarithmic, but only values which are multiples of 10 are displayed in the graph, but I need values for all gridlines to be shown in the graph.

X-Y_graph

As can be seen in the picture, only 10^6 is displayed on the x-axis, but I need values for other points to also be displayed.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
khashayar
  • 11
  • 3

1 Answers1

1
  • Tested in python 3.11.4, matplotlib 3.7.1
  • As per the duplicates:
    • This answer verifies all major and minor ticks are displayed.
    • This answer shows how to format and show the minor xtick labels.
  • As the plot shows, including the minor x-tick labels crowds the x-axis.
    • This answer shows how to show specific minor xtick labels, by selectively setting subs (e.g. subs=[.2, .4, .6, .8]).
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
import numpy as np

y = np.arange(11)
x = 10.0**y

fig, ax = plt.subplots(figsize=(25, 6))

ax.semilogx(x, y)

# show all minor and major xticks, and all major xtick labels
ax.xaxis.set_major_locator(tkr.LogLocator(numticks=999))
ax.xaxis.set_minor_locator(tkr.LogLocator(numticks=999, subs="all"))

# remove this line to remove the minor xtick labels
ax.xaxis.set_minor_formatter(tkr.FormatStrFormatter('%d'))

enter image description here


  • There are many questions on Stack Overflow, which discuss the use of customized formatters.
    • .set_minor_formatter
    • .set_major_formatter
  • Swapping x and y, setting the minor tick label format, and the font size, can make for an improved visual experience.
y = np.arange(11)
x = 10.0**y

fig, ax = plt.subplots(figsize=(10, 20))

# swap x and y
ax.semilogy(y, x)

ax.yaxis.set_major_locator(tkr.LogLocator(numticks=999))
ax.yaxis.set_minor_locator(tkr.LogLocator(numticks=999, subs="all"))

# adjust the minor label format
mkfunc = lambda x, pos: '%1.0fM' % (x * 1e-6) if x >= 1e6 else '%1.0fK' % (x * 1e-3) if x >= 1e3 else '%1.0f' % x
mkformatter = tkr.FuncFormatter(mkfunc)
ax.yaxis.set_minor_formatter(mkformatter)

# adjust the font size
ax.yaxis.set_tick_params(which='minor', labelsize=5)
ax.yaxis.set_tick_params(which='major', labelsize=8)

# set the y limits
ax.set_ylim(1, 10**10)

# save figure
fig.savefig('logscale.png')

enter image description here


y = np.arange(11)
x = 10.0**y

fig, ax = plt.subplots(figsize=(25, 6))
ax.semilogx(x, y)

ax.xaxis.set_major_locator(tkr.LogLocator(numticks=999))
ax.xaxis.set_minor_locator(tkr.LogLocator(numticks=999, subs="all"))

mkfunc = lambda x, pos: '%1.0fM' % (x * 1e-6) if x >= 1e6 else '%1.0fK' % (x * 1e-3) if x >= 1e3 else '%1.0f' % x
mkformatter = tkr.FuncFormatter(mkfunc)
ax.xaxis.set_minor_formatter(mkformatter)

# set the minor xtick label rotation (in this case, the implicit pyplot command is easiest)
plt.xticks(rotation=90, ha='right', minor=True)

# adjust the font size
ax.xaxis.set_tick_params(which='minor', labelsize=5)
ax.xaxis.set_tick_params(which='major', labelsize=8)

ax.set_xlim(1, 10**10)

fig.savefig('logscale.png')

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158