You can use the Axes.set_yscale
method. That allows you to change the scale after the Axes
object is created. That would also allow you to build a control to let the user pick the scale if you needed to.
The relevant line to add is:
ax.set_yscale('log')
You can use 'linear'
to switch back to a linear scale. Here's what your code would look like:
import pylab
import matplotlib.pyplot as plt
a = [pow(10, i) for i in range(10)]
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)
line, = ax.plot(a, color='blue', lw=2)
ax.set_yscale('log')
pylab.show()
I finally found some time to do some experiments in order to understand the difference between them. Here's what I discovered:
log
only allows positive values, and lets you choose how to handle negative ones (mask
or clip
).
symlog
means symmetrical log, and allows positive and negative values.
symlog
allows to set a range around zero within the plot will be linear instead of logarithmic.
I think everything will get a lot easier to understand with graphics and examples, so let's try them:
import numpy
from matplotlib import pyplot
# Enable interactive mode
pyplot.ion()
# Draw the grid lines
pyplot.grid(True)
# Numbers from -50 to 50, with 0.1 as step
xdomain = numpy.arange(-50,50, 0.1)
# Plots a simple linear function 'f(x) = x'
pyplot.plot(xdomain, xdomain)
# Plots 'sin(x)'
pyplot.plot(xdomain, numpy.sin(xdomain))
# 'linear' is the default mode, so this next line is redundant:
pyplot.xscale('linear')
# How to treat negative values?
# 'mask' will treat negative values as invalid
# 'mask' is the default, so the next two lines are equivalent
pyplot.xscale('log')
pyplot.xscale('log', nonposx='mask')
# 'clip' will map all negative values a very small positive one
pyplot.xscale('log', nonposx='clip')
# 'symlog' scaling, however, handles negative values nicely
pyplot.xscale('symlog')
# And you can even set a linear range around zero
pyplot.xscale('symlog', linthreshx=20)
Just for completeness, I've used the following code to save each figure:
# Default dpi is 80
pyplot.savefig('matplotlib_xscale_linear.png', dpi=50, bbox_inches='tight')
Remember you can change the figure size using:
fig = pyplot.gcf()
fig.set_size_inches([4., 3.])
# Default size: [8., 6.]
(If you are unsure about me answering my own question, read this)
Best Answer
They all do different things, since matplotlib uses a hierarchical order in which a figure window contains a figure which may consist of many axes. Additionally, there are functions from the pyplot interface and there are methods on the
Figure
class. I will discuss both cases below.pyplot interface
pyplot
is a module that collects a couple of functions that allow matplotlib to be used in a functional manner. I here assume thatpyplot
has been imported asimport matplotlib.pyplot as plt
. In this case, there are three different commands that remove stuff:See
matplotlib.pyplot
Functions:plt.cla()
clears an axes, i.e. the currently active axes in the current figure. It leaves the other axes untouched.plt.clf()
clears the entire current figure with all its axes, but leaves the window opened, such that it may be reused for other plots.plt.close()
closes a window, which will be the current window, if not specified otherwise.Which functions suits you best depends thus on your use-case.
The
close()
function furthermore allows one to specify which window should be closed. The argument can either be a number or name given to a window when it was created usingfigure(number_or_name)
or it can be a figure instancefig
obtained, i.e., usingfig = figure()
. If no argument is given toclose()
, the currently active window will be closed. Furthermore, there is the syntaxclose('all')
, which closes all figures.methods of the Figure class
Additionally, the
Figure
class provides methods for clearing figures. I'll assume in the following thatfig
is an instance of aFigure
:fig.clf()
clears the entire figure. This call is equivalent toplt.clf()
only iffig
is the current figure.fig.clear()
is a synonym forfig.clf()
Note that even
del fig
will not close the associated figure window. As far as I know the only way to close a figure window is usingplt.close(fig)
as described above.