Combine two figures with/without spines and ticks using Python and matplotlib.pyplot


The result is:

Combine two figures with/without spines and ticks using Python and matplotlib.pyplot

This code shows how to combine two figures with and without spines and ticks using Python and matplotlib.pyplot.

See also:

Python Matplotlib Tips: Remove ticks and spines (box around figure) using Python and maptlotlib.pyplot

This page shows how to remove ticks and spines (box around figure) using Python and matplotlib.pyplot.

Python Matplotlib Tips: Add second x-axis below first x-axis using Python and matplotlib.pyplot

This page shows how to draw second x-axis below the first x-axis. Using "twinx", "set_ticks_position", and "set_label_position", and "spines['bottom'].set_position", you can move the second x-axis from the top of the figure to below the first x-axis.


In [1]:
from platform import python_version as pythonversion
print('python: '+pythonversion())
import matplotlib.pyplot as plt
from matplotlib import __version__ as matplotlibversion
print('matplotlib: '+matplotlibversion)
python: 3.6.3
matplotlib: 3.0.1

Define data

In [2]:
x = [0,1,2,3,4,5,6]
y = [1,5,2,8,3,5,2]
In [3]:
fig = plt.figure(figsize=(4,3))
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.plot(x,y)
ax2.plot(x,y)
ax2.tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False)
ax2.tick_params(axis='y', which='both', right=False, left=False, labelleft=False)
for pos in ['right','top','bottom','left']:
    ax2.spines[pos].set_visible(False)
plt.show()