To obtain the correct orientation of the contour plot and save the plots as seperate images when created in a loop?

℡╲_俬逩灬. 提交于 2019-12-11 13:42:54

问题


I have written a code that uses griddata interpolation to interpolate points on an irregular grid onto a regular grid. I also creating contour plots for two dependent variables after the interpolation. I am able to generate the line contour and filled contour plots and they also match the images qualitatively that were directly generated from my simulation. But the images are shown in a horizontal orientation and also the quality is not as per my expectation. I want the images to be shown as vertical i.e (x-axis; width =30.48(always fixed),y-axis; width varies according to a condition). In the plots x and y are interchanged. I did not understand the reason for this. I think it has to do with how the rows and columns are handled by contour function.

My second issue is that I am importing multiple text files and I want to save the generated contour plots separately for each of the imported text files. I am not sure of the best way to do this.

from __future__ import print_function
import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate  import griddata
###############################################################################
path = 'location of the files'
FT_init = 5.4311
delt = 0.15
TS_init = 140
dj_length = 2.4384
###############################################################################
def streamfunction2d(y,x,Si_f):
    plt.figure(3)
    Stf= plt.contour(y,x,Si_f,50)
    Stf1 = plt.colorbar(Stf)
    plt.savefig('location of the saved image',format='png',dpi=1200)
    plt.close(3)

###############################################################################    
def tracer2d(y,x,Ti_f):
    plt.figure(1)
    trc= plt.contour(y,x,Ti_f,25)
    trc1 = plt.colorbar(trc)
    plt.clabel(trc,fmt='%.0f',inline=True)
    plt.savefig('location of the saved image',format='png',dpi=1200)
    plt.close(1)

    plt.figure(2)
    trp = plt.pcolormesh(y,x,Ti_f,linestyle='dashed')  ## Alternate method of creating filled-contour plots. 
    plt.savefig('location of the saved image',format='png',dpi=1200)
    plt.close(2) 
###############################################################################
flowtime =np.linspace(500,600,2) ## Here I would include multiple flow times. So I want to save the plots for each flow time in the location provided.
timestep = np.zeros(len(flowtime))

for n in range(len(flowtime)):
    timestep = (flowtime-FT_init)/delt + TS_init
    timestep = np.array(np.round(timestep,-2),dtype = 'int')

for p in range(len(timestep)):

        timestepstring=str(timestep[p]).zfill(4)
        fname = path+"ddn150AE-"+timestepstring+".txt"
        f = open(fname,'r')
        data = np.loadtxt(f,skiprows=1)
        data = data[data[:, 1].argsort()]
        data = data[np.logical_not(data[:,11]== 0)] 

        Y  = data[:,2]  
        limit = np.nonzero(Y==dj_length)[0][0]
        Y  = Y[limit:]

        Vf  = data[:,11] 
        Vf  = Vf[limit:]
        Tr  = data[:,9] 
        Tr  = Tr[limit:]

        X  = data[:,1]  
        X  = X[limit:]
        Y  = data[:,2] 
        Y  = Y[limit:]
        U  = data[:,3]  
        U  = U[limit:]
        V  = data[:,4]  
        V  = V[limit:]
        St = data[:,5]  
        St  = St[limit:]        
###############################################################################     

### The following code will do the interpolation for the dependent variable

        ## Using griddata for interpolation from Unstructured to Structured data
        # resample onto a 100x100 grid

        nx, ny = 100,100

        # (N, 2) arrays of input x,y coords and dependent values
        pts = np.vstack((X,Y )).T
        vals = np.vstack((Tr))
        vals1 = np.vstack((St))

        # The new x and y coordinates for the grid
        x = np.linspace(X.min(), X.max(), nx)
        y = np.linspace(Y.min(), Y.max(), ny)
        r = np.meshgrid(y,x)[::-1]

        # An (nx * ny, 2) array of x,y coordinates to interpolate at
        ipts = np.vstack(a.ravel() for a in r).T

        # An (nx * ny, 2) array of interpolated Tr values
        Ti = griddata(pts, vals, ipts, method='nearest')
        Ti_f = np.reshape(Ti,(len(y),len(x)))
        tracer2d(y,x,Ti_f)

        Si = griddata(pts, vals1, ipts, method='nearest')
        Si_f = np.reshape(Si,(len(y),len(x)))
        streamfunction2d(y,x,Si_f)

Location for the text files Sample Text Files. This is the image that I have from simulation corresponding to 3400 text file.

I would appreciate any advice or help in this.

Thank you

SOLVED:

来源:https://stackoverflow.com/questions/34834109/to-obtain-the-correct-orientation-of-the-contour-plot-and-save-the-plots-as-sepe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!