You can do this with a twin Axes, which is not set to loglog scale. In this case, we want to make both twin x and y axes, so we can stack the commands, as shown here:
ax4 = ax3.twinx().twiny()
An alternative is to just create a new Axes instance in the same position as the first (from @ImportanceOfBeingErnest in the comments). For example:
ax4 = fig.add_subplot(111, label="ax4")
We also need to make ax3
transparent so we can see through it to the image below (facecolor='None'
).
We also need to set the zorder to ax3
is on top of ax4
.
Here is a working example:
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()
