RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 922409
Accepted
Sergey
Sergey
Asked:2020-12-18 17:13:56 +0000 UTC2020-12-18 17:13:56 +0000 UTC 2020-12-18 17:13:56 +0000 UTC

如何从表中绘制两个函数

  • 772

有一些csv格式的实验数据表。我像这样导入它:

>>> import pandas as pd
>>> df = pd.read_csv('data.csv', sep=',')
>>> df
    'Set'     'Rx'     'Traffic'     'Modulation'
0   -67.0   -64.35         15.00     '64-QAM 2/3'
1   -68.0   -65.35         15.00     '16-QAM 3/4'
2   -69.0   -66.85         14.78     '64-QAM 2/3'
3   -70.0   -67.60         15.42     '64-QAM 2/3'
4   -71.0   -68.85         15.04     '64-QAM 2/3'
5   -72.0   -70.35         15.04     '16-QAM 3/4'
6   -73.0   -70.85         15.04     '16-QAM 3/4'
7   -74.0   -71.35         15.28     '16-QAM 3/4'
8   -75.0   -72.60         12.35     '64-QAM 2/3'
9   -76.0   -73.10         11.38     '16-QAM 3/4'
10  -77.0   -74.60         11.64     '16-QAM 3/4'
11  -78.0   -75.60          7.76     '16-QAM 1/2'
12  -79.0   -76.85          7.76     '16-QAM 1/2'
13  -80.0   -77.85          7.52     '16-QAM 1/2'
14  -81.0   -79.35          5.85       'QPSK 3/4'

现在我想绘制一个使用“设置”列作为 X 值的图表。在这张图上,我想看到两条(!)曲线:

  • Y1:'Rx'
  • Y2:“交通”

matplotlib.pyplot如何做到这一点?我找到了一个指南(Pandas),第一步是从表中选择一列,如下所示:

df = pd.read_csv('apple.csv', index_col='Date', parse_dates=True)
new_sample_df = df.loc['2012-Feb':'2017-Feb', ['Close']]

但是我的第一个数据框索引是数字的,我无法选择我感兴趣的列:

new_sample_df = df.iloc[0:14, ['Rx']]
TypeError: cannot perform reduce with flexible type

但即使我能以某种方式做到这一点,我如何将每个 X 点的两个(!) Y 值传递给绘图函数?尝试了两个建议的选项。它不起作用:-(为了消除歧义,我给出了 CSV 文件本身:

'Set,Rx','Traffic','Modulation'
-67.00, -64.35,    15.00,   '64-QAM 2/3'
-68.00, -65.35,    15.00,   '16-QAM 3/4'
-69.00, -66.85,    14.78,   '64-QAM 2/3'
-70.00, -67.60,    15.42,   '64-QAM 2/3'
-71.00, -68.85,    15.04,   '64-QAM 2/3'
-72.00, -70.35,    15.04,   '16-QAM 3/4'
-73.00, -70.85,    15.04,   '16-QAM 3/4'
-74.00, -71.35,    15.28,   '16-QAM 3/4'
-75.00, -72.60,    12.35,   '64-QAM 2/3'
-76.00, -73.10,    11.38,   '16-QAM 3/4'
-77.00, -74.60,    11.64,   '16-QAM 3/4'
-78.00, -75.60,     7.76,   '16-QAM 1/2'
-79.00, -76.85,     7.76,   '16-QAM 1/2'
-80.00, -77.85,     7.52,   '16-QAM 1/2'
-81.00, -79.35,     5.85,     'QPSK 3/4' 

以及我正在尝试执行的程序的文本,注释掉了两个选项:

#!/usr/bin/env python

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv(r'data.csv', quotechar="'")
#1 df.set_index('Set')[['Rx','Traffic']].plot(legend=True, grid=True)
#2 df.set_index("'Set'")[["'Rx'","'Traffic'"]].plot(legend=True, grid=True)

如果取消注释 #1,将显示以下消息:

KeyError: 'Set'

如果您取消注释 #2,则消息会相应更改:

KeyError: "'Set'"

也许重点是我是第三条蟒蛇,而例子是第二条?

python
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    MaxU - stop genocide of UA
    2020-12-18T19:52:01Z2020-12-18T19:52:01Z

    要正确读取 CSV,其中值和列名用引号括起来,您应该使用参数quotechar=...:

    In [26]: df = pd.read_csv(r'C:\Temp\data.csv', quotechar="'")
    
    In [27]: df
    Out[27]:
         Set     Rx  Traffic  Modulation
    0  -67.0 -64.35    15.00  64-QAM 2/3
    1  -68.0 -65.35    15.00  16-QAM 3/4
    2  -69.0 -66.85    14.78  64-QAM 2/3
    3  -70.0 -67.60    15.42  64-QAM 2/3
    4  -71.0 -68.85    15.04  64-QAM 2/3
    5  -72.0 -70.35    15.04  16-QAM 3/4
    6  -73.0 -70.85    15.04  16-QAM 3/4
    7  -74.0 -71.35    15.28  16-QAM 3/4
    8  -75.0 -72.60    12.35  64-QAM 2/3
    9  -76.0 -73.10    11.38  16-QAM 3/4
    10 -77.0 -74.60    11.64  16-QAM 3/4
    11 -78.0 -75.60     7.76  16-QAM 1/2
    12 -79.0 -76.85     7.76  16-QAM 1/2
    13 -80.0 -77.85     7.52  16-QAM 1/2
    14 -81.0 -79.35     5.85    QPSK 3/4
    
    In [28]: df.columns
    Out[28]: Index(['Set', 'Rx', 'Traffic', 'Modulation'], dtype='object')
    

    之后,您可以构建如下图:

    df.set_index('Set')[['Rx','Traffic']].plot(legend=True, grid=True)
    

    在此处输入图像描述

    CSV 文件示例:

    'Set','Rx','Traffic','Modulation'
    -67.0,-64.35,15.0,'64-QAM 2/3'
    -68.0,-65.35,15.0,'16-QAM 3/4'
    -69.0,-66.85,14.78,'64-QAM 2/3'
    -70.0,-67.6,15.42,'64-QAM 2/3'
    -71.0,-68.85,15.04,'64-QAM 2/3'
    -72.0,-70.35,15.04,'16-QAM 3/4'
    -73.0,-70.85,15.04,'16-QAM 3/4'
    -74.0,-71.35,15.28,'16-QAM 3/4'
    -75.0,-72.6,12.35,'64-QAM 2/3'
    -76.0,-73.1,11.38,'16-QAM 3/4'
    -77.0,-74.6,11.64,'16-QAM 3/4'
    -78.0,-75.6,7.76,'16-QAM 1/2'
    -79.0,-76.85,7.76,'16-QAM 1/2'
    -80.0,-77.85,7.52,'16-QAM 1/2'
    -81.0,-79.35,5.85,'QPSK 3/4'
    
    • 3
  2. MaxU - stop genocide of UA
    2020-12-18T18:43:07Z2020-12-18T18:43:07Z
    df.set_index("'Set'")[["'Rx'","'Traffic'"]].plot(legend=True)
    

    在此处输入图像描述

    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5