Note that there are some explanatory texts on larger screens.

plurals
  1. POCopying contours from one axes instance to another
    primarykey
    data
    text
    <p>I have a situation where I am producing upwards of 20 different images using matplotlib. This is done many many times. Each of the 20 images has the same set of contours in the background. In order to reduce the processing time, it would be useful to be able to copy the result of <code>countourf()</code> from one <code>Axes</code> instance to another. In order to do this, I have tried this:</p> <pre><code>#!/bin/env python import os import numpy as np from matplotlib import pyplot as plt def copycontours(): #Create figures fig1 = plt.figure() fig2 = plt.figure() fig3 = plt.figure() #Create axes ax1 = fig1.add_axes((0.05,0.05,0.90,0.90)) ax2 = fig2.add_axes((0.05,0.05,0.90,0.90)) ax3 = fig3.add_axes((0.05,0.05,0.90,0.90)) #Create random data data = np.random.normal(25, size=(25,25)) #Add contours to first axes instance and save image contours = ax1.contourf(data) fig1.savefig('test.png') #Add contours to second axes instance from first axes instance for collection in ax1.collections: ax2.add_collection(collection) fig2.savefig('test2.png') #Add contours to third axes instance from for collection in contours.collections: ax3.add_collection(collection) fig3.savefig('test3.png') os.system('display test.png &amp;') os.system('display test2.png &amp;') os.system('display test3.png &amp;') if __name__ == '__main__': copycontours() </code></pre> <p>The first figure (test.png) comes out looking correct. The axes range from 0 to 25 and the full domain is filled. </p> <p><img src="https://i.stack.imgur.com/FUz08.png" alt="test1.png"></p> <p>The other two (test2.png, test3.png) come out differently. Their axes range from 0 to 1 and the contour region only fills the area from 0.0 to approximately 7.9.</p> <p><img src="https://i.stack.imgur.com/eRwa0.png" alt="test2.png"></p> <p>Resetting the axis limits via <code>ax2.set_xlim(0,25)</code> and <code>ax2.set_xlim(0,25)</code> changes the axis ranges, but does not fix the larger problem.</p> <p><img src="https://i.stack.imgur.com/QN1mE.png" alt="test3.png"></p> <p>Does anyone have a thought on how to fix this problem or another method for reusing the results of <code>contourf()</code> in a different way?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload