I am trying to use an existing graph as a background for new data that I want to plot on top of the graph.
I have been able to do so when using a graph with all information contained within the axes and using the extent
parameter of plt.imshow
because then I just have to scale the image.
I would like to scale and shift the background graph. Replotting the background is not an option in the real use case.
Here is what I tried so far :
- Generation of the background graph (reproducible example)
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 5, 10], [8, 5, 12])
ax.set_xlim(0, 20)
ax.set_ylim(0, 15)
ax.set_title('Background graph')
fig.show()
fig.savefig('bg_graph.png')
- Use
plt.imshow()
to add the background graph and then superimpose my data.
bg_img = plt.imread('bg_graph.png')
fig, ax = plt.subplots()
ax.imshow(bg_img, extent=[0,50,0,50])
ax.scatter([4.9, 5.2], [7, 4.9])
fig.show()
fig.savefig('result.png')
I have made a mockup of the expected result using Excel :
Is there a method to stretch a new graph onto existing axis (from an image) in order to plot new pieces of data ? I assume that the coordinates of the axis are known or can be guessed through trial an error.