首页 > 解决方案 > Python 值错误。发酵地块需要更多的工作

问题描述

我正在尝试编写一个程序,该程序从 excel 文件中获取列并绘制它们。该图有多个轴,是来自发酵的数据。我在绘图代码方面得到了帮助,但我无法让它与数据一起工作。python 是否将列作为字符串读取?代码如下:

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")

和错误:

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)

带有 5 个 y 轴的发酵图的图像

标签: pythonmatplotlibplot

解决方案


您发布了大约 75 行来支持 5 行问题。请更新您的帖子。

问题很简单。 plot返回组成图形的线列表。您的分配解包列表并将第一个元素分配给p1,但不涵盖列表的其他元素。尾随的逗号指定您将只采用该一个元素,而不是将整个列表分配给p1. 你打算用这个语句来完成什么?如果该列表包含除单个元素之外的任何内容,这将保证运行时错误。

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

推荐阅读