7

I have vales with very small difference like... 0.000001. I want to visualize them on logarithmic scale. I am wondering how to do it in matplotlib.

Thanks a lot

Shan
  • 18,563
  • 39
  • 97
  • 132
  • Possible duplicate of [Plot logarithmic axes with matplotlib in python](http://stackoverflow.com/questions/773814/plot-logarithmic-axes-with-matplotlib-in-python) – bluenote10 Apr 03 '17 at 08:42

4 Answers4

18

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axis

Simply add the keyword argument log=True

Or, in an example:

from matplotlib import pyplot
import math
pyplot.plot([x for x in range(100)],[math.exp(y) for y in range(100)] )
pyplot.xlabel('arbitrary')
pyplot.ylabel('arbitrary')
pyplot.title('arbitrary')

#pyplot.xscale('log')
pyplot.yscale('log')

pyplot.show()

enter image description here

Nick
  • 2,342
  • 28
  • 25
Cory Dolphin
  • 2,650
  • 1
  • 20
  • 30
3

Since all other answers only mention the global pyplot.xscale("log") approach: You can also set it per axis, but then the syntax is:

ax.set_yscale("log")
bluenote10
  • 23,414
  • 14
  • 122
  • 178
2

You can use this piece of code:

import matplotlib.pyplot
# to set x-axis to logscale
matplotlib.pyplot.xscale('log')
# to set y-axis to logscale
matplotlib.pyplot.yscale('log')
Thanasis Petsas
  • 4,378
  • 5
  • 31
  • 57
2

Instead of plot, you can use semilogy:

import numpy as npy
import matplotlib.pyplot as plt
x=npy.array([i/100. for i in range(100)])
y=npy.exp(20*x)
plt.semilogy(x, y)
plt.show()

But I'm not entirely sure what you hope to gain from using a log scale. When you say "small difference", do you mean that the values might be something like 193.000001 and 193.000002? If so, it might help to subtract off 193.

Mike
  • 19,114
  • 12
  • 59
  • 91