Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Others by (19.9k points)
recategorized by

I have this huge master excel file, and I am unpacking a few variables, and my code is expectedly not working that well. I am using Spyder for Python, and it says that there is an ordered dictionary for a few variables, and as I go to unpack the variables to plot the data its not working. Thanks in advance for your help. The Lactose are the sheet names and I am getting the data in those columns and plotting them a simple operation I thought...but it wasn't. My issue is that I am trying to use for loops to collect data from a master excel file, then use another for loop to grab that data and plot it. The task is to the following it should run like this read Sheet1, Sheet2, Sheet3, and so on columns A,B, M,N (not exact columns or sheetnames and then plot that data and produce pairs of graphs. The problem that is actually occurring is that it won't assign title or produce a total of 16 graphs paired into 8 sections.

import pandas as pd

import scipy as sp

from scipy import constants

import numpy as np

import matplotlib.pyplot as plt

d = ['Lactose.450g.2p', 'Lactose.229g.16p', 'Lactose.343g.16p', 

 'Lactose.375g.2p','Lactose.400g.16p', 'Lactose.419g.16p', 

 'Lactose.425g.2p', 'Lactose.500g.2p']

for i in d:

       reference = []

       list_reference  = pd.read_excel('C:\\Kean\\MasterFileLactose071019.xlsx', sheet_name = [i])

       reference.append(list_reference)

       xaxis_samp = pd.read_excel('C:\\Kean\\MasterFileLactose071019.xlsx', sheet_name = [i], usecols = 'C' , skiprows = [0,1])

       yaxis_samp = pd.read_excel('C:\\Kean\\MasterFileLactose071019.xlsx', sheet_name = [i], usecols = 'D' , skiprows = [0,1])

       xaxis_ref = pd.read_excel('C:\\Kean\\MasterFileLactose071019.xlsx', sheet_name = [i], usecols = 'I', skiprows = [0,1])

       yaxis_ref = pd.read_excel('C:\\Kean\\MasterFileLactose071019.xlsx', sheet_name = [i], usecols ='J', skiprows = [0,1])

       for e1, e2, e3, e4, e5 in zip(xaxis_samp,yaxis_samp,xaxis_ref,yaxis_ref, [i]):

           fig_ref, [ax1, ax2] = plt.subplots(nrows = 2, ncols = 1, figsize = (10,10))

           ax1.plot(e3, e4)

           ax1.set_title( 'Reference Spectrum Teflon Tablet')

           ax1.grid(True)

           ax1.set_xlim(0,3)

           ax1.set(xlabel = 'TeraHertz (THz)', ylabel = 'Electric field (a.u.)')

           ax2.plot(e1,e2)

           ax2.set_title('Sample Spectrum ' + str(e5) + ' Tablet')

           ax2.grid(True)

           ax2.set_xlim(0,3)

           ax2.set(xlabel = 'TeraHertz (THz)', ylabel = 'Electric field (a.u.)')

1 Answer

0 votes
by (25.1k points)

Just add ax2.plot(xaxis_samp[e1],yaxis_samp[e2]) before your last 4th line. This will set the x axis and y axis as you want them.

Related questions

0 votes
1 answer
asked Dec 10, 2020 in Python by laddulakshana (16.4k points)
0 votes
1 answer
+1 vote
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...