Home > Software engineering >  Drawing a logarithmic number line
Drawing a logarithmic number line

Time:12-07

sorry I'm quite new with Python in general.

Does matplotlib have a feature to plot just an axis, a one-dimensional picture if you want? I need a logarithmic number line ranging from about 8 to 25 with markings, just like the ones on the old slider calculators if you remember.

Would be great if the line could be curved as well, not a necessity.

CodePudding user response:

np.logspace() should get you what you need, as long as you don't mind a 2d box where your numberline sits.

import numpy as np
import matplotlib.pyplot as pp
val = 0. # this is the value where you want the data to appear on the y-axis.
ar = np.logspace(np.log10(8), np.log10(25)) # just as an example array
pp.plot(ar, np.zeros_like(ar)   val, 'x')
pp.show()

CodePudding user response:

You can create a figure and hide all but the x axis. Making an axis logarithmic is also doable. Here's an example that may be what you're looking for.

import matplotlib.pyplot as plt

plt.figure(figsize=(8, 2))
ax = plt.subplot()

# hide all but lower axis spine
ax.spines['right'].set_color('none')
ax.spines['left'].set_color('none')
ax.spines['top'].set_color('none')

# put x axis spine in the center of the figure
ax.spines['bottom'].set_position('center')

# hide y ticks
ax.tick_params(left=False, labelleft=False)

# define limits of number line
ax.set_xlim(8, 25)

# place ticks logarithmically
ax.set_xscale('log')
ax.set_xticks(range(8, 26))

# add own tick labels
ax.set_xticklabels(range(8, 26))

plt.show()

This produces the following:

logarithmic x axis

  • Related