Figure
is the bounding box within which plot elements appear.Figure
can contain one or more Axes
objects, each of which in turn contains other objects representing plot contents.xaxis
and yaxis
, which in turn have attributes that contain all the properties of the lines, ticks, and labels that make up the axes.# In[1]
import matplotlib.pyplot as plt
plt.style.use('classic')
import numpy as np
%matplotlib inline
# In[2]
ax=plt.axes(xscale='log',yscale='log')
ax.set(xlim=(1,1E3),ylim=(1,1E3))
ax.grid(True);
formatter
and locator
objects of each axis.# In[3]
print(ax.xaxis.get_major_locator())
print(ax.xaxis.get_minor_locator())
print('\n')
print(ax.xaxis.get_major_formatter())
print(ax.xaxis.get_minor_formatter())
# Out[3]
<matplotlib.ticker.AutoLocator object at 0x7f5ae12edf00>
<matplotlib.ticker.NullLocator object at 0x7f5ae12eeb60>
<matplotlib.ticker.ScalarFormatter object at 0x7f5ae12eebc0>
<matplotlib.ticker.NullFormatter object at 0x7f5ae12ef160>
plt.NullLocator
and plt.NullFormatter
.# In[4]
ax=plt.axes()
rng=np.random.default_rng(1701)
ax.plot(rng.random(50))
ax.grid()
ax.yaxis.set_major_locator(plt.NullLocator())
ax.xaxis.set_major_formatter(plt.NullFormatter())
# In[5]
fig,ax=plt.subplots(5,5,figsize=(5,5))
fig.subplots_adjust(hspace=0,wspace=0)
# get some face data from Scikit-Learn
from sklearn.datasets import fetch_olivetti_faces
faces=fetch_olivetti_faces().images
for i in range(5):
for j in range(5):
ax[i,j].xaxis.set_major_locator(plt.NullLocator())
ax[i,j].yaxis.set_major_locator(plt.NullLocator())
ax[i,j].imshow(faces[10 * i + j],cmap='binary_r')
# In[6]
fig,ax=plt.subplots(4,4,sharex=True,sharey=True)
plt.MaxNLocator
, which allows us to specify the maximum number of ticks that will be displayed.# In[7]
# for every axis, set the x and y major locator
for axi in ax.flat:
axi.xaxis.set_major_locator(plt.MaxNLocator(3))
axi.yaxis.set_major_locator(plt.MaxNLocator(3))
fig
plt.MultipleLocator
.# In[8]
# plot a sine and cosine curve
fig,ax=plt.subplots()
x=np.linspace(0,3 * np.pi,1000)
ax.plot(x,np.sin(x),lw=3,label='Sine')
ax.plot(x,np.cos(x),lw=3,label='Cosine')
# set up grid, legend, and limits
ax.grid(True)
ax.legend(frameon=False)
ax.axis('equal')
ax.set_xlim(0,3 * np.pi);
MultipleLocator
, which locates ticks at a multiple of the number we provide.# In[9]
ax.xaxis.set_major_locator(plt.MultipleLocator(np.pi / 2))
ax.xaxis.set_minor_locator(plt.MultipleLocator(np.pi / 4))
fig
plt.FuncFormatter
, which accepts a user-defined function giving fine-grained control over the tick outputs.# In[10]
def format_func(value, tick_number):
# find number of multiple of pi/2
N=int(np.round(2*value/np.pi))
if N==0:
return "0"
elif N==1:
return r"$\pi/2$"
elif N==2:
return r"$\pi$"
elif N % 2 > 0:
return rf"${N}\pi/2$"
else:
return rf"${N//2}\pi$"
ax.xaxis.set_major_formatter(plt.FuncFormatter(format_func))
fig
Matplotilb locator options
Locator class | Description |
---|---|
NullLocator | No ticks |
FixedLocator | Tick locations are fixed |
IndexLocator | Locator for index plots |
LinearLocator | Evenly spaced ticks from min to max |
LogLocator | Logarithmically spaced ticks from min to max |
MultipleLocator | Ticks and range are a multiple of base |
MaxNLocator | Finds up to a max number of ticks at nice locations |
AutoLocator | (Default) MaxNLocator with simple defaults |
AutoMinorLocator | Locator for minor ticks |
Matplotlib formatter options
Formatters class | Description |
---|---|
NullFormatter | No labels on the ticks |
IndexFormatter | Set the strings from a list of labels |
FixedFormatter | Set the strings manually for the labels |
FuncFormatter | User-designed function sets the labels |
FormatStrFormatter | Use a format string for each value |
ScalarFormatter | Default formatter for scalar values |
LogFormatter | Default formatter for log axes |