【问题标题】:Error in plotting time series data with plotly, truth value is ambiguous用 plotly 绘制时间序列数据时出错,真值不明确
【发布时间】:2019-08-29 04:20:41
【问题描述】:

我正在尝试使用 plotly express 来绘制时间序列数据。

我收到此错误。我的代码中没有任何布尔值,所以我不明白为什么它无法处理此代码。

ValueError:索引的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

我查看了数据帧的数据类型并转置并尝试编辑时间轴数据。 dtype 是 dtype('0')。

import pandas as pd
import numpy as np
import plotly_express as px

df = pd.read_excel('FreewayFDSData.xlsx', 'Volume')
df = df.set_index("Row Labels")
detector =df.columns
time = df.index

px.scatter(df, x=detector,y=time)

不幸的是,最终结果是错误代码。

https://www.plotly.express/

df.head().to_dict()

{'3674S_P1': {'00:00 - 00:15': 88.0,
  '00:15 - 00:30': 116.0,
  '00:30 - 00:45': 84.0,
  '00:45 - 01:00': 68.0,
  '01:00 - 01:15': 76.0},
 '3674S_P0': {'00:00 - 00:15': 88.0,
  '00:15 - 00:30': 116.0,
  '00:30 - 00:45': 76.0,
  '00:45 - 01:00': 68.0,
  '01:00 - 01:15': 76.0},
 '3674M_P1': {'00:00 - 00:15': 84.0,
  '00:15 - 00:30': 108.0,
  '00:30 - 00:45': 68.0,
  '00:45 - 01:00': 68.0,
  '01:00 - 01:15': 72.0},
 '3674M_P0': {'00:00 - 00:15': 92.0,
  '00:15 - 00:30': 108.0,
  '00:30 - 00:45': 68.0,
  '00:45 - 01:00': 68.0,
  '01:00 - 01:15': 72.0},
 '3674EL_P1': {'00:00 - 00:15': 96.0,
  '00:15 - 00:30': 112.0,
  '00:30 - 00:45': 64.0,
  '00:45 - 01:00': 64.0,
  '01:00 - 01:15': 88.0},
 '3674EL_P0': {'00:00 - 00:15': 108.0,
  '00:15 - 00:30': 124.0,
  '00:30 - 00:45': 68.0,
  '00:45 - 01:00': 80.0,
  '01:00 - 01:15': 100.0},
 '14549IB_P1': {'00:00 - 00:15': 380.0,
  '00:15 - 00:30': 412.0,
  '00:30 - 00:45': 380.0,
  '00:45 - 01:00': 328.0,
  '01:00 - 01:15': 320.0},
 '14549IB_P0': {'00:00 - 00:15': 376.0,
  '00:15 - 00:30': 408.0,
  '00:30 - 00:45': 376.0,
  '00:45 - 01:00': 328.0,
  '01:00 - 01:15': 328.0},
 '14548IB_P1': {'00:00 - 00:15': 464.0,
  '00:15 - 00:30': 508.0,
  '00:30 - 00:45': 448.0,
  '00:45 - 01:00': 408.0,
  '01:00 - 01:15': 376.0},
 '14548IB_P0': {'00:00 - 00:15': 464.0,
  '00:15 - 00:30': 508.0,
  '00:30 - 00:45': 444.0,
  '00:45 - 01:00': 404.0,
  '01:00 - 01:15': 376.0},
 '14547IB_P1': {'00:00 - 00:15': 501.33333333333337,
  '00:15 - 00:30': 528.0,
  '00:30 - 00:45': 501.33333333333337,
  '00:45 - 01:00': 432.0,
  '01:00 - 01:15': 437.33333333333337},
 '14547IB_P0': {'00:00 - 00:15': 460.0,
  '00:15 - 00:30': 520.0,
  '00:30 - 00:45': 440.0,
  '00:45 - 01:00': 396.0,
  '01:00 - 01:15': 380.0},
 '14546IB_P1': {'00:00 - 00:15': 468.0,
  '00:15 - 00:30': 536.0,
  '00:30 - 00:45': 440.0,
  '00:45 - 01:00': 396.0,
  '01:00 - 01:15': 380.0},
 '14546IB_P0': {'00:00 - 00:15': 464.0,
  '00:15 - 00:30': 532.0,
  '00:30 - 00:45': 436.0,
  '00:45 - 01:00': 396.0,
  '01:00 - 01:15': 376.0},
 '14545IB_P1': {'00:00 - 00:15': 464.0,
  '00:15 - 00:30': 524.0,
  '00:30 - 00:45': 448.0,
  '00:45 - 01:00': 400.0,
  '01:00 - 01:15': 384.0},
 '14545IB_P0': {'00:00 - 00:15': 468.0,
  '00:15 - 00:30': 516.0,
  '00:30 - 00:45': 444.0,
  '00:45 - 01:00': 396.0,
  '01:00 - 01:15': 380.0},
 '14544IB_P1': {'00:00 - 00:15': 440.0,
  '00:15 - 00:30': 516.0,
  '00:30 - 00:45': 440.0,
  '00:45 - 01:00': 396.0,
  '01:00 - 01:15': 380.0},
 '14544IB_P0': {'00:00 - 00:15': 444.0,
  '00:15 - 00:30': 512.0,
  '00:30 - 00:45': 444.0,
  '00:45 - 01:00': 392.0,
  '01:00 - 01:15': 364.0},
 '14543IB_P1': {'00:00 - 00:15': 456.0,
  '00:15 - 00:30': 524.0,
  '00:30 - 00:45': 440.0,
  '00:45 - 01:00': 396.0,
  '01:00 - 01:15': 364.0},
 '14543IB_P0': {'00:00 - 00:15': 452.0,
  '00:15 - 00:30': 528.0,
  '00:30 - 00:45': 444.0,
  '00:45 - 01:00': 400.0,
  '01:00 - 01:15': 368.0},
 '14542IB_P1': {'00:00 - 00:15': 444.0,
  '00:15 - 00:30': 516.0,
  '00:30 - 00:45': 432.0,
  '00:45 - 01:00': 396.0,
  '01:00 - 01:15': 368.0},
 '14542IB_P0': {'00:00 - 00:15': 444.0,
  '00:15 - 00:30': 528.0,
  '00:30 - 00:45': 428.0,
  '00:45 - 01:00': 396.0,
  '01:00 - 01:15': 356.0},
 '14540IB_P1': {'00:00 - 00:15': 384.0,
  '00:15 - 00:30': 412.0,
  '00:30 - 00:45': 352.0,
  '00:45 - 01:00': 340.0,
  '01:00 - 01:15': 300.0},
 '14540IB_P0': {'00:00 - 00:15': 384.0,
  '00:15 - 00:30': 408.0,
  '00:30 - 00:45': 348.0,
  '00:45 - 01:00': 332.0,
  '01:00 - 01:15': 304.0},
 '14537IB_P1': {'00:00 - 00:15': 420.0,
  '00:15 - 00:30': 420.0,
  '00:30 - 00:45': 340.0,
  '00:45 - 01:00': 330.0,
  '01:00 - 01:15': 300.0},
 '14537IB_P0': {'00:00 - 00:15': 530.0,
  '00:15 - 00:30': 565.0,
  '00:30 - 00:45': 455.0,
  '00:45 - 01:00': 445.0,
  '01:00 - 01:15': 420.0},
 '14536IB_P1': {'00:00 - 00:15': 296.0,
  '00:15 - 00:30': 260.0,
  '00:30 - 00:45': 300.0,
  '00:45 - 01:00': 212.0,
  '01:00 - 01:15': 260.0},
 '14536IB_P0': {'00:00 - 00:15': 304.0,
  '00:15 - 00:30': 256.0,
  '00:30 - 00:45': 290.0,
  '00:45 - 01:00': 208.0,
  '01:00 - 01:15': 256.0},
 '14405WB_P1': {'00:00 - 00:15': 504.0,
  '00:15 - 00:30': 520.0,
  '00:30 - 00:45': 456.0,
  '00:45 - 01:00': 428.0,
  '01:00 - 01:15': 376.0},
 '14405WB_P0': {'00:00 - 00:15': 504.0,
  '00:15 - 00:30': 524.0,
  '00:30 - 00:45': 448.0,
  '00:45 - 01:00': 420.0,
  '01:00 - 01:15': 376.0}}

【问题讨论】:

  • 你介意分享df.head().to_dict()吗?
  • df.head().to_dict()

标签: python pandas plotly


【解决方案1】:

我认为您不能使用 plotly_express 解决您的问题,因为 xy 应该是字符串。检查 px.scatter 的输出?

px.scatter?
data_frame: A 'tidy' `pandas.DataFrame`
x: (string, name of column in `data_frame`) Values from this column are used to position marks along the x axis in cartesian coordinates.
y: (string, name of column in `data_frame`) Values from this column are used to position marks along the y axis in cartesian coordinates.

鉴于安装 plotly_express 您也可以使用 plotly

import plotly.offline as py
import plotly.graph_objs as go
import pandas as pd

# here I'm using your df.head().to_dict() as dct
df = pd.DataFrame(dct)
traces = [go.Scatter(x=df.index,
                     y=df[col],
                     name=col)
          for col in df.columns]


py.iplot(traces)

【讨论】:

  • 大家好,我是 Plotly Express 的作者,现在确实如此。我们正在考虑增加传递 df.index 或 df.my_column 的能力...github.com/plotly/plotly_express/issues/37 中的讨论
  • 同时我添加了更严格的验证和更好的错误消息来避免这个问题:)
【解决方案2】:

现在在我的手机上,但想尽快提供帮助,所以我稍后会处理正确的格式。

数据类型肯定是这里的问题。您正在设置time = df.index,稍后您将时间分配给px.scatter(df, x=detector,y=time) 中的y。而df.index 的第一个值是36745_P1。因此,据我所知,这是你的问题。你真的想绘制 x 和 y 的什么值?

【讨论】:

  • 如果可能的话,我想把它们都画出来,所以所有检测器都与所有时间相比
  • 要使用 Plotly Express,您必须将数据重塑为具有三列:时间、检测器、值。对于您拥有的数据形状,您可能希望使用cufflinks 来执行df.iplot(...)
猜你喜欢
  • 2021-06-27
  • 2020-04-19
  • 2017-07-14
  • 1970-01-01
  • 2020-04-09
  • 2018-11-11
  • 1970-01-01
  • 2016-02-09
相关资源
最近更新 更多