Tuesday, 31 July 2018

Covering 2D plots with 3D surface in python

I am attempting to take many xz plots, each at various y values, and overlay a surface. I have seen many examples of how to plot 3D surfaces in python, but aside from this post, nothing seems to match my inquiry very closely.

The image of what I need to do is shown below (Note: ignore the "constant x" - this is due to a more complicated variable layout than I explained here):

Magnitude vs. Frequency

My code is as follows, and simply takes data and plots each of the individual Magnitude vs. Frequency plots (xz plots):

import numpy as np
import glob, os
import codecs
import re
import matplotlib.pyplot as plt
#-------------------------------------------------------------------------                   
os.chdir('C:/Users/elarrick/Desktop/201807161056')
dataFolders = glob.glob('./*.ltda')

dataLines = []
freq = []
OpenLoopMag = []
OpenLoopPhase = []
for item in dataFolders:
    name, ext = os.path.splitext(item)
    if ext == '.ltda':
        print item
        dataLines = []
        f = codecs.open(item, encoding='utf-8')
        for line in f:
            if '<p>' in line:
                dataLines.append(line)      #All lines with <p> are an entry in dataLines
        #print "\n\n", dataLines
        #break
        for item in dataLines:
            item = re.sub("<p>", "", item)
            item = re.sub("True</p>", "", item)
            item = item.replace(",", "")
            splitItem = item.split()
            #print splitItem
            freq.append(float(splitItem[0]))
            OpenLoopMag.append(float(splitItem[1]))
            OpenLoopPhase.append(float(splitItem[2]))
        print "Frequencies: ", freq
        print "\n\n\n\n\n\nOpenLoopMag: ", OpenLoopMag
#   This is where I will make the plots for each x,y position
        name = name.strip(".\\")
        name = name.replace("-","NEG")
        plt.semilogx(freq, OpenLoopMag)
        #plt.plot(freq, OpenLoopMag)
        plt.xlabel("Frequency, (Hz)")
        plt.ylabel("Magnitude")
        plt.title("{0}".format(name))
        plt.xlim([20,2000])
        #plt.ylim([-43.2,10.9])
        ticks = [20,40,70,100,200,400,700,1000,2000]
        plt.xticks(ticks,ticks)
        plt.savefig("plot_{0}.png".format(name))

#________ Clear the values for the next data folder_______#            
        freq = []
        OpenLoopMag = []
        OpenLoopPhase = []
        break
    else:
        print "Something went wrong - check ColorMap.py"
        sys.exit()

The next thing I need is to grab each plot, find the y value at which the data was taken, and plot along a y axis (shown coming out of the page in the previous picture). Can you help me do that?

Edit: Using Jacob Madsen's code shown below, I get the following graph.

Edit Inclusion



from Covering 2D plots with 3D surface in python

No comments:

Post a Comment