Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set ticks with logarithmic scale

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?

like image 273
Ruggero Turra Avatar asked Jan 25 '13 20:01

Ruggero Turra


People also ask

How do you change a scale to a log scale?

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.

What is the meaning of logarithmic 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.


2 Answers

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() 

resulting plot

like image 70
tacaswell Avatar answered Sep 17 '22 08:09

tacaswell


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() 

enter image description here

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() 

enter image description here

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() 

enter image description here

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() 

enter image description here

like image 31
Franck Dernoncourt Avatar answered Sep 18 '22 08:09

Franck Dernoncourt