Вы можете сделать это с двумя осями, для которых не задан масштаб журнала. В этом случае мы хотим создать обе оси x и y, чтобы мы могли составлять команды, как показано здесь :
ax4 = ax3.twinx().twiny()
Альтернативой является создание нового экземпляра Axes в той же позиции, что и первый (из @ ImportanceOfBeingErnest в комментариях). Например:
ax4 = fig.add_subplot(111, label="ax4")
Нам также нужно сделать ax3
прозрачным, чтобы мы могли видеть сквозь него изображение ниже (facecolor='None'
).
Нам также нужно установить zorder на ax3
поверх ax4
.
Вот рабочий пример:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
# Dummy data
rf_layer = 0.1 + np.random.rand(20) * 9.9
qc_layer = 1. + np.random.rand(20) * 99.
fig2 = plt.figure()
# Make ax3 transparent so we can see image behind
ax3 = fig2.add_subplot(1, 1, 1, facecolor='None')
pathimage='./stinkbug.png'
img = mpimg.imread(pathimage)
ax3.scatter(rf_layer, qc_layer)
ax3.set_title('my title', y=1.1, fontsize=12)
ax3.set_yscale('log')
ax3.set_xscale('log')
ax3.set_ylim([1, 100])
ax3.set_xlim([0.1, 10])
ax3.set_xlabel('Rf in %')
ax3.set_ylabel('qc in MPa')
# Create second axes
ax4 = ax3.twinx().twiny()
# Or alternatively
# ax4 = fig.add_subplot(111, label="ax4")
# Add image to twin axes
ax4.imshow(img)
# Fix zorder so ax3 on top of ax4
ax3.set_zorder(10)
ax4.set_zorder(1)
# Turn off ticks from twin axes
ax4.set_yticks([])
ax4.set_xticks([])
plt.show()
