It seems that the set_xticks
is not working in log scale:
from matplotlib import pyplot as plt fig1, ax1 = plt.subplots() ax1.plot([10, 100, 1000], [1,2,3]) ax1.set_xscale('log') ax1.set_xticks([20, 200, 500]) plt.show()
is it possible?
Select the "Scale" tab on the Format Axis window. Click the "Logarithmic Scale" check box near the bottom of the window. This changes the chart's axis to a log scale.
Definition of logarithmic scale : a scale on which the actual distance of a point from the scale's zero is proportional to the logarithm of the corresponding scale number rather than to the number itself — compare arithmetic scale.
import matplotlib from matplotlib import pyplot as plt fig1, ax1 = plt.subplots() ax1.plot([10, 100, 1000], [1,2,3]) ax1.set_xscale('log') ax1.set_xticks([20, 200, 500]) ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
or
ax1.get_xaxis().get_major_formatter().labelOnlyBase = False plt.show()
I'm going to add a few plots and show how to remove the minor ticks:
The OP:
from matplotlib import pyplot as plt fig1, ax1 = plt.subplots() ax1.plot([10, 100, 1000], [1,2,3]) ax1.set_xscale('log') ax1.set_xticks([20, 300, 500]) plt.show()
To add some specific ticks, as tcaswell pointed out, you can use matplotlib.ticker.ScalarFormatter
:
from matplotlib import pyplot as plt import matplotlib.ticker fig1, ax1 = plt.subplots() ax1.plot([10, 100, 1000], [1,2,3]) ax1.set_xscale('log') ax1.set_xticks([20, 300, 500]) ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) plt.show()
To remove the minor ticks, you can use matplotlib.rcParams['xtick.minor.size']
:
from matplotlib import pyplot as plt import matplotlib.ticker matplotlib.rcParams['xtick.minor.size'] = 0 matplotlib.rcParams['xtick.minor.width'] = 0 fig1, ax1 = plt.subplots() ax1.plot([10, 100, 1000], [1,2,3]) ax1.set_xscale('log') ax1.set_xticks([20, 300, 500]) ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) plt.show()
You could use instead ax1.get_xaxis().set_tick_params
, it has the same effect (but only modifies the current axis, not all future figures unlike matplotlib.rcParams
):
from matplotlib import pyplot as plt import matplotlib.ticker fig1, ax1 = plt.subplots() ax1.plot([10, 100, 1000], [1,2,3]) ax1.set_xscale('log') ax1.set_xticks([20, 300, 500]) ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) ax1.get_xaxis().set_tick_params(which='minor', size=0) ax1.get_xaxis().set_tick_params(which='minor', width=0) plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With