Plot multiple data using for loop, pyplot and genfromtxt - python

I am pretty sure this particular problem must have been treated somewhere but I cannot find it so I put the question.
I have 66 files with data stored in one single column. I wish to plot all data in a single plot. I'm used to do it with bash where acquiring and plotting data inside a loop is pretty trivial but I can't figure out in python.
thanks a lot for your help.
NM

Something like this should do it, although it will depend on how your data files are named.
import matplotlib.pyplot as plt
import numpy as np
fig,ax = plt.subplots()
# Lets say your files are called data-00.txt, data-01.txt etc.
for i in range(66):
data=np.genfromtxt('data-{:02d}.txt'.format(i))
ax.plot(data)
fig.savefig('my_fig.png')

Related

Plotting multiple graphs from multiple text files in python

I have multiple text files in a directory. The 1st line of each text file is the header line. Rest of the lines are like columns containing different datas. I have to plot 7th column vs 5th column data graphs for each text file. I also want to plot all the graphs using a loop and a single code. Can anyone pls help me to do this? Thank you in advance.
You can use pandas and matplotlib.pyplot
import matplotlib.pyplot as plt
import pandas as pd
# sep= accepts the separator of your data i.e. ' ' space ',' comma etc
table = pd.read_csv('your_file_name.txt', sep=' ')
table.plot(x=['header_of_5th_col',y=['header_of_7th_col'])
I suggest also to check pandas documentations about loading data and plot them
You can then loop the table.plot line of code to plot every graph you need
code for getting all files in a specified directory:
import os
files = os.listdir("path/to/directory")
print(files)
for reading the files I would suggest the library pandas (here) and for plotting matplotlib (here).
for a more detailed solution more information on what exact data is given and what output is expected is needed.
for example sharing the first few lines of one of the files and a basic image created in paint or similar containing what things should roughly look like.

How do I make the ploy show in my df analysis

I have a dataframe of emails that has three columns: From, Message and Received (which is a date format).
I've written the below script to show how many messages there are per month in a bar plot.
But the plot doesn't show and I can't work out why, it's no doubt very simple. Any help understanding why is much appreciated!
Thanks!
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('XXX')
df = df[df['Message'].notna()]
df['Received'] = pd.to_datetime(df['Received'], format='%d/%m/%Y')
df['Received'].groupby(df['Received'].dt.month).count().plot
A pyplot object (commonly plt) is not shown until you call plt.show(). It is designed that way so you can create your plot and then modify it as needed before showing or saving.
Also checkout plt.savefig().

plotting using pandas in python

What i am trying to do is slightly basic, however i am very new to python, and am having trouble.
Goal: is to plot the yellow highlighted Row(which i have highlighted, however it will not be highlighted when i need to read the data) on the Y-Axis and plot the "Time" Column on the X-Axis.
Here is a photo of the Data, and then the code that i have tried along with its error.
Code
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
#Reading CSV and converting it to a df(Data_Frame)
df1 = pd.read_csv('Test_Sheet_1.csv', skiprows = 8)
#Creating a list from df1 and labeling it 'Time'
Time = df1['Time']
print(Time)
#Reading CSV and converting it to a df(Data_Frame)
df2 = pd.read_csv('Test_Sheet_1.csv').T
#From here i need to know how to skip 4 lines.
#I need to skip 4 lines AFTER the transposition and then we can plot DID and
Time
DID = df2['Parameters']
print(DID)
Error
As you can see from the code, right now i am just trying to print the Data so that i can see it, and then i would like to put it onto a graph.
I think i need to use the 'skiplines' function after the transposition, so that python can know where to read the "column" labeled parameters(its only a column after the Transposition), However i do not know how to use the skip lines function after the transposition unless i transpose it to a new Excel Document, but this is not an option.
Any help is very much appreciated,
Thank you!
Update
This is the output I get when I add print(df2.columns.tolist())

Python OpenPyxl inserting Histogram to Excel

I have been using OpenPyxl for creating Excel workbooks using data from other CSV files.
Currently I want to insert a histogram into the worksheet based on a numerical list that I have as variable x below.
I cannot find an efficient way to generate the histogram, the option I opted for was generate the histogram in matplotlib save and then place in worksheet, however this seems combersome and I feel like I am missing some synthax to directly pass the plt to the img.
The option using Reference seems imperfect also as I have 10^6 length vectors and would rather not write them to this file.
import numpy as np
import openpyxl
import matplotlib.pyplot as plt
wb = openpyxl.Workbook()
ws = wb.active
x = np.random.rand(100)
plt.clf()
plt.hist(x, bins=10)
plt.savefig('temp1.png')
img = openpyxl.drawing.image.Image('temp1.png',size=(300,300))
img.anchor(ws.cell('A1'))
ws.add_image(img)
plt.clf()
plt.plot(x)
plt.savefig('temp2.png')
img = openpyxl.drawing.image.Image('temp2.png',size=(300,300))
img.anchor(ws.cell('A15'))
ws.add_image(img)
wb.save("trial.xlsx")
As you can see this generates two .png files and overall seems unclean. I do not think the preformance is taking much of a hit but undoubtedly will have better solutions and optimization is valued here.
I would treat answers of the form: "Swap from using OpenPyxl to ..." as a last resort only.

How do I import data in scientific notation for a plot?

So far I have been trying to import poorly formated scientific notation data into a plot using python. I have tried variants of
import matplotlib.pyplot as plt
import numpy as np
data = np.genfromtxt("/home/rex/Documents/ModLab/PhotoElec/Exp2Data.csv",delimiter=',', unpack=True, names=True)
plt.axis([-1,32, 0 , 203])
plt.title('All Aperture')
plt.xlabel('Voltage')
plt.ylabel('Current')
plt.plot(data, 'ro')
plt.show()
The data is in a csv file and looks like this but far longer.
I2,V2,I2,V2,I2,V2
0,-0.5,0,-1,0,-0.9
2.00E-011,0.5,1.00E-010,0,3.50E-010,0.1
5.00E-011,1.5,3.00E-010,1,1.55E-009,1.1
Also when I run the assign data file I get this weird error.
SyntaxError: EOL while scanning string literal
Any help would be appreciated.
I think your problem is in the actual import - it may have been imported as a string, not a number.
I'd suggest using pandas to handle the import. If you're doing scientific computing, you'll find pandas very useful. Your problem then becomes:
import pandas
data = pandas.read_csv('Exp2Data.csv')
i2 = data.I2
v2 = data.V2
# ... plot as needed
There may also be ways for pandas to handle the plotting as well!
I think that the problem comes from your header. Your header (first line) is a string and obviously, matplotlib can't plot this kind of data.
You have to make :
data = np.genfromtxt("/home/rex/Documents/ModLab/PhotoElec/Exp2Data.csv",
delimiter=',',
unpack=True,
names=True,
skip_header = 1)
# skip_header let to go directly to your data and pass the header
I didn't run the script but it should work ;)

Categories