Christoph
Christoph

Reputation: 1090

plot over image, where extent is not yet known when plotting image

I want to create a figure that shows a background image with overlaid scatter and line plots:

The curve fit probably isn't the best you've seen. That's ok.

As you can see, the axes ticks show image coordinates. The scatter and line plot are given in image coordinates, too - which is not desired. The scatter and line plots should still be able to work (and be meaningful) without the background image. The extent is not known because this figure is used to determine the extent (interactively) in the first place.

Instead, I'd like to specify the scatter and line plots in the coordinate system shown in the background image (units m³/h and m): the transformation from image coordinates to "axis on top" coordinates would be roughly (110,475) -> (0,10) and (530,190) -> (8,40).

In principle I can see two ways of doing it:

Upvotes: 0

Views: 292

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339590

The restriction here seems to be that "The scatter and line plots should still be able to work (and be meaningful) without the background image.". This however would not imply that you cannot use the extent keyword argument.

At the time you add the image, you'd specify the extent.

plt.scatter(...)
plt.plot(...)
plt.imshow(..., extent = [...])

You can also set the extent later, if that is desired for some reason not explained in the question, i.e.

plt.scatter(...)
plt.plot(...)
im = plt.imshow(...)

im.set_extent([...])

Finally you may also decide to remove the image, and plot it again; this time with the desired extent,

plt.scatter(...)
plt.plot(...)
im = plt.imshow(...)

im.remove()
im = plt.imshow(..., extent=[...])

Upvotes: 1

Related Questions