I want to recreate a plot in Python from a dataset that looks like the figure below.
It has four columns (time1, power1, time2, power2), tab delimiters and contains about 5000 rows.
Text file with data
It should look like the following;
Instead it looks like this in Python;
Obviously I am making mistakes. I am quite new to python so it might be a dumb fault. My code is as follows;
# Import
import numpy as np
import pandas as pd
import csv
import matplotlib.pyplot as plt
# Load data from csv file
Filename = "powercurveHTC10000_2.txt"
with open(Filename) as inp:
Dataoverview = list(zip(*(line.strip().split('\t') for line in inp)))
print(list(zip(*(line.strip().split('\t') for line in inp))) )
#Data1 = Dataoverview[1]
Time1 = Dataoverview[0][0:5000]
Power1 = Dataoverview[1][0:5000]
Time2 = Dataoverview[3][0:5000]
Power2 = Dataoverview[4][0:5000]
print('probeer', Dataoverview[3][0:2])
# Plot
plt.plot(Time1, Power1, 'ro')
# plt.plot(Time2, Power2, 'b*')
plt.title('Power usage measured by thermocouples')
plt.ylim(0,100)
plt.xlim(3000,4250)
plt.gca().set_xticks([2])
plt.gca().set_yticks([2])
plt.xlabel('Cycles [s]')
plt.ylabel('Power [W]')
plt.show()
Love to heat what I did wrong. Thank you in advance.
I see you’re importing pandas in your code but not using it.
Have you tried something like this?
df = pd.read_csv('powercurveHTC10000_2.txt', delimiter=" ")
df.plot()
plt.show()
Please supply a sample of the data so we can copy it. Image is no good. Or a link to the data if online.
When you read data from a file like this, it comes in as strings. You possibly just need to convert the strings to floats. This is a common mistake: matplotlib.org/stable/users/…
You could also have a look at numpy.loadtxt numpy.org/doc/stable/reference/generated/numpy.loadtxt.html