简体   繁体   English

Matplotlib:空白 plot 和 window 不会关闭

[英]Matplotlib: blank plot and window won't close

I'm trying to plot a curve using the data from a csv file using:我正在尝试使用 csv 文件中的数据来 plot 曲线:

import matplotlib.pyplot as plt
from csv import reader

with open('transmission_curve_HST_ACS_HRC.F606W.csv', 'rw') as f:
         data = list(reader(f))
         wavelength_list = [i[0] for i in data[1::]]
         percentage = [i[1] for i in data[1::]]

plt.plot(wavelength_list, percentage)
plt.show()

But all it make is opening a completely blank window and I can't close it unless I close the terminal.但它所做的只是打开一个完全空白的 window ,除非我关闭终端,否则我无法关闭它。

The csv file looks like this: csv 文件如下所示:

4565,"0,00003434405472044760"
4566,"0,00004045191689260860"
4567,"0,00004656394357747830"
4568,"0,00005267963655205460"
4569,"0,00005879949856084820"

Do you have any idea why?你知道为什么吗?

You need to modify three things in your code:您需要在代码中修改三件事:

  1. Change 'rw' to 'r' when you read from the file从文件中读取时将'rw'更改为'r'
  2. Correct the way you iterate over data更正迭代数据的方式
  3. Convert the numbers from the second column to float将第二列中的数字转换为float

import matplotlib.pyplot as plt
from csv import reader

with open('transmission_curve_HST_ACS_HRC.F606W.csv', 'r') as f:
         data = list(reader(f))
         wavelength_list = [i[0] for i in data]
         percentage = [float(str(i[1]).replace(',','.')) for i in data]

plt.plot(wavelength_list, percentage)
plt.show()

Content of the csv file: csv文件内容:

4564,"0,00002824029270045730"
4565,"0,00003434405472044760"
4566,"0,00004045191689260860"
4567,"0,00004656394357747830"
4568,"0,00005267963655205460"
4569,"0,00005879949856084820"

在此处输入图像描述

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

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