0

I am attempting to graph battery cycling data similar to this . Each line is one cycle worth of datapoints and should be one line on the graph. At first the code I wrote simply saw the dataframe as a continuous variable, then I inserted a for loop that would graph 1 line for the 1 cycles worth of data, iterate to the next cycle 2 but currently it simply bugs and does not show any graph. Debug seems to show an issue once it loops past cycle 1. Each cycle does not have an equal amount of data points.

EDIT: I suspect now when looping the headers of the data is causing an issue. I think making a dictionary would solve this issue

df2 = pd.read_excel(r'C:\Users\####\- ##### - ####\2-7_7.xlsx',\
                   sheet_name='record', usecols="A:N")
    
df2['Capacity(mAh)'] = df2['Capacity(mAh)'].apply(lambda x: x*1000) #A fix for unit error in the data

df2.set_index('Cycle ID',inplace = True) #Set the index to the Cycle number

for cycle in df2.index:
    chosen_cyclex = df2.loc[cycle, 'Capacity(mAh)']
    chosen_cycley = df2.loc[cycle,'Voltage(V)']
    plt.plot(chosen_cyclex.iloc[1],chosen_cycley.iloc[1])
    #print(chosen_cyclex[1],chosen_cycley[1])


plt.show()
2
  • The link is broken. Please post a screenshot of whatever you were trying to show, or something similar. Commented Apr 18, 2022 at 19:24
  • Edited with a new link from Google. Commented Apr 18, 2022 at 19:29

2 Answers 2

1

I ended up using this method, where the equivalents were selected.

  for cycle in cyclearray:
        plt.plot(df2[df2.index == cycle]['Capacity(mAh)'],df2[df2.index == cycle]['Voltage(V)'],cycle

For other battery testers who show up here, if you need to 'cut' the voltages curves up, use

plt.xlim([xmin,xmax])
plt.ylim([ymin+0.1,ymax-0.1])
Sign up to request clarification or add additional context in comments.

Comments

0

You need to specify an ax when plotting. Here are some examples:

# reproducible (but unimaginative) setup
n = 100
cycles = 4

df2 = pd.DataFrame({
    'ID': np.repeat(np.arange(cycles), n),
    'Capacity(mAh)': np.tile(np.arange(n), cycles),
    'Voltage(V)': (np.arange(n)**0.8 * np.linspace(5, 3, cycles)[:, None]).ravel(),
})

Example 1: using groupby.plot, then fiddle around to adjust labels

fig, ax = plt.subplots()
df2.groupby('ID').plot(x='Capacity(mAh)', y='Voltage(V)', ax=ax)

# now customize the labels
lines, labels = ax.get_legend_handles_labels()
for ith, line in zip('1st 2nd 3rd 4th'.split(), lines):
    line.set_label(f'{ith} discharge')
ax.legend()

Example 2: groupby used as an iterator

fig, ax = plt.subplots()

ld = {1: 'st', 2: 'nd', 3: 'rd'}
for cycle, g in df2.groupby('ID'):
    label = f'{cycle + 1}{ld.get(cycle + 1, "th")} discharge'
    g.plot(x='Capacity(mAh)', y='Voltage(V)', label=label, ax=ax)

Same plot as above.

Example 3: using ax.plot instead of df.plot or similar

fig, ax = plt.subplots()

ld = {1: 'st', 2: 'nd', 3: 'rd'}
for cycle, g in df2.groupby('ID'):
    label = f'{cycle + 1}{ld.get(cycle + 1, "th")} discharge'
    ax.plot(g['Capacity(mAh)'], g['Voltage(V)'], label=label)
ax.legend()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.