-2

I am trying to write a program that takes columns from an excel file and plots them. The plot has multiple axes, and is data from a fermentation. I have had help with the code for the plot, but I cannot get it to work with the data. Is python reading the columns as strings? Here comes the code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# with the given sample data
data = {'Time': [0, 1, 2, 3], 'Temp': [28.05, 29.0, 28.65, 27.04], 'Agit': [400, 397, 402, 430], 'DO': [71.0, 5.0, 5.5, 2.0], 'pH': [5.5, 5.2, 4.9, 4.75], 'GasFlow': [1.0, 1.02, 1.01, 1.05]}

df = pd.DataFrame(data)

# insert the name of the column as a string in brackets
Time = list(df['Time'])
pH = list(df['pH']) 
DO = list(df['DO'])
Agit = list(df['Agit'])
GasFlow = list(df['GasFlow'])
Temp = list(df['Temp'])
In [5]:
#%% Writing  data

time = [Time] # Hours

temperature = [Temp] # dC

agitation = [Agit] # rpm

DO = [DO] # %

pH = [pH] # pH

gas_flow = [GasFlow]  #vvm

# Plot the data
fig, host = plt.subplots(figsize=(8,5)) # (width, height) in inches

p1, = host.plot(time, temperature,    color=color1, label="Temperature")

And the error:

ValueError                                Traceback (most recent call last)
<ipython-input-5-97e016dc1996> in <module>
     72 # Plot the data
     73 
---> 74 p1, = host.plot(time, temperature,    color=color1, label="Temperature")
     75 
     76 p2, = par1.plot(time, agitation,    color=color2, label="Agitation")

ValueError: too many values to unpack (expected 1)

An image of a fermentation plot with 5 y axes

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. If you don't know whether the data is read as strings, then check the type. This code is not minimal -- 150 lines is excessive for the problem. – Prune May 13 '21 at 15:20
  • Sorry, I am new at this. I clipped everything below the error causing line. I appreciate any help and I will do what is necessary to get better at posting, and coding (within reason) – BubbaZinetti May 13 '21 at 17:12
  • Everything where you do `time = [Time]` is not required and is causing the issue. You can do `p1, = host.plot(Time, Temp, color=color1, label="Temperature")` instead. Or you can do `p1, = host.plot(df.Time, df.Temp, color=color1, label="Temperature")` and also remove all of the section where you do `Time = list(df['Time'])`, which is also unnecessary. – Trenton McKinney May 13 '21 at 19:47

1 Answers1

0

You posted about 75 lines to support a 5-line problem. Please update your post.

The problem is straightforward. plot returns a list of lines that comprise the graph. Your assignment unpacks the list and assigns the first element to p1, but doesn't cover the other elements of the list. The trailing comma specifies that you will take only that one element, rather than assigning the entire list to p1. Just what did you intend this statement to accomplish? This guarantees a run-time error if that list has anything other than a single element.

p1, = host.plot(time, temperature,    color=color1, label="Temperature")
Prune
  • 76,765
  • 14
  • 60
  • 81
  • Yes, can you offer a suggestion as to how I can get the plot to cover the other elements of the list. I am going to spend some time tonight working on this, so I will see if I can solve it myself before looking here in the morning. Thanks. – BubbaZinetti May 13 '21 at 19:15
  • In order to help more, I need you to complete your post: update according to the links and comments I've made, and then address the open issues I presented. – Prune May 13 '21 at 19:17