bballboy8
bballboy8

Reputation: 450

How do I superimpose an image in the back of a matplotlib plot?

I'm trying to superimpose an image in the back of a matplotlib plot. It is being rendered as HTML in a flask website so I am saving the plot as an image before inserting it. The plot without the background image looks like this:

enter image description here

The code that produces the above output is here:

        fname = 'scatter_averages.png'
        url_full = os.path.join(_path_full, fname)
        image = plt.imread("app/static/images/quantum_grid.jpg")
        if os.path.isfile(url_full):
            os.remove(url_full)
        plt.clf()
        df = self.text_numeric_averages()
        if df is not None:
            plt.figure(figsize=(6, 8))
            fig, ax = plt.subplots()
            df.index += 1
            x, y = df.iloc[:, 2], df.iloc[:, 1]
            ax.plot(x, y, '-o')
            plt.xlabel(df.columns[2])
            plt.ylabel(df.columns[1])
            for i in range(len(df)):
                xyi = df.iloc[i, :].values
                ax.annotate(str(df.index[i]) + " " + xyi[0][:3], (xyi[2], xyi[1]))
            axes = plt.gca()
            y_min, y_max = axes.get_ylim()
            x_min, x_max = axes.get_xlim()
            # ax.imshow(image, extent=[x_min, x_max, y_min, y_max])
            plt.savefig(url_full)

The commented out line above is my attempt to get the image to superimpose. The output when that line is uncommented is this:

enter image description here

How do I keep the sizing and scale of the first image but use the background image in the second plot as the background? I'm not concerned with the image looking distorted.

Upvotes: 7

Views: 282

Answers (1)

bballboy8
bballboy8

Reputation: 450

ax.imshow(image, extent=[x_min, x_max, y_min, y_max], aspect="auto")

This will fix it.

Upvotes: 6

Related Questions