简体   繁体   English

matplot情节未显示图形

[英]matplot the plot is not showing the graph

import csv
import matplotlib.pyplot as plt

path="E:\Google Stock Market Data - google_stock_data.csv.csv"
file=open(path)
reader=csv.reader(file)
a=[]
b=[]
header=next(reader)
data=[]
for row in reader:
    data.append(row[:])
    a=row[1]
    b=row[2]
plt.plot(a,b)
plt.show()

I tried a CSV using the script above, but when I try to plot it, it is shows the following error: 我使用上面的脚本尝试了CSV,但是当我尝试绘制它时,它显示以下错误:

File "C:\Users\user\Anaconda3\lib\site-packages\matplotlib\colors.py", line 204, in _to_rgba_no_colorcycle
raise ValueError("RGBA values should be within 0-1 range")

ValueError: RGBA values should be within 0-1 range

<matplotlib.figure.Figure at 0x1b5e93b5ba8>

What does this mean and how to fix it? 这是什么意思,以及如何解决?

You are not creating lists for a and b , and the elements you are using are not numbers. 您没有为ab创建列表,并且所使用的元素不是数字。 A CSV file is just text, even if that text is something like "16.35" . CSV文件就是文本,即使该文本类似于"16.35"

You can fix both by changing 您可以通过更改两者来解决

a=row[1]
b=row[2]

to

a.append(float(row[1]))
b.append(float(row[2]))

To answer the first part of your question, take a look at the docs for plt.plot . 要回答问题的第一部分,请查看plt.plot的文档。 Your input parameters are both strings, so the signature you are matching is most likely plot(y, 'r+') . 您的输入参数都是字符串,因此您要匹配的签名很可能是plot(y, 'r+') The error is telling you that some particular part of your supposed color spec is nonsense. 该错误告诉您,假定的颜色规格的某些特定部分是胡说八道。 Passing in floats will fix that. 传递浮点数将解决此问题。

As a nitpick, try to use with blocks for doing file I/O. 作为nitpick,尝试with块一起使用来执行文件I / O。 This will guarantee that your file is closed even if an error occurs. 这样可以确保即使发生错误也可以关闭文件。 Instead of file=open(...) , do 代替file=open(...)

with open(path) as file:
    reader = csv.reader(file)
    a = b = []
    header = next(reader)
    data = []
    for row in reader:
        data.append(row)
        a.append(float(row[1]))
        b.append(float(row[2]))
plt.plot(a,b)
plt.show()

You may notice that I changed row[:] to row . 您可能会注意到,我将row[:]更改为row There is no need to make a copy if the reference is not used anywhere else. 如果引用没有在其他地方使用,则无需进行复制。

You loop has to be like: 您的循环必须像:

for row in reader:
    data.append(row[:])
    a.append(row[1]) # to add element to list use append
    b.append(row[2])
plt.plot(a,b)
plt.show()

You have to plot after filling lists a and b . 您必须在填充列表ab之后绘图。

I recommend you to plot stock data with candlestick like here: https://stackoverflow.com/a/33068041/2666859 我建议您使用烛台来绘制库存数据,例如: https : //stackoverflow.com/a/33068041/2666859

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM