【问题标题】:Read two columns and draw the graph读取两列并绘制图形
【发布时间】:2017-12-07 14:55:43
【问题描述】:

我已经问过我的问题:How to read two columns using python,但我的目标是绘制一个图形oX = f(oY)

我试过了:

import matplotlib.pyplot as plt
from matplotlib import style    
import csv

with open('file.csv') as f:
    reader = csv.reader(f)
    for row in reader:
        res = row[0].split()
        oX = res[0]
        oY = res[1]
        print (oX , oY)

它对我不起作用。

【问题讨论】:

  • 不起作用是什么意思?这种描述对我们没有帮助。
  • 嗨 eyllanesc,我想将 oX 和 oY 转换为列表,以绘制图表
  • 请参阅this question,了解如何将分类变量绘制为散点图。

标签: python csv matplotlib


【解决方案1】:

仅当您使用 matplotlib 2.1.0 或更高版本时,才可以使用 scatter 绘制标签(字符串)。 2.1.0 是撰写此答案时的最新稳定版本。

import pandas as pd
import json
from matplotlib import pyplot as plt

import csv
x = []
y = []
with open('e:/projects/data.txt') as f:
    reader = csv.reader(f)
    for row in reader:
        res = row[0].split()
        oX = res[0]
        oY = res[1]
        x.append(oX)
        y.append(oY)
        print (oX, oY)

使用您的原始代码仅绘制输入文件中的最后一个坐标。您需要将oXoY 存储在一个列表中,例如xy

plt.scatter (x, y, linewidth = 0.1, color = "black", label = 'V')
plt.show ()

使用您其他问题的输入,您的图表应如下所示

【讨论】:

  • 嗨,Jaco,谢谢,我将安装 anaconda3,它可以工作吗?
  • 你的输出(图片),正是我想做的
  • 我必须使用pip install matplotlib。 Conda 仍在安装 3.0.0。然而,这可能是由于我的 conda 频道的顺序。
  • 谢谢JACO
猜你喜欢
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-21
  • 1970-01-01
  • 1970-01-01
  • 2015-05-03
相关资源
最近更新 更多