我正在尝试为名为“count_backers.csv”的csv文件创建一个简单的直方图。我到目前为止的代码如下:
import matplotlib.pyplot as plt
# import numpy as np
import plotly.plotly as py
import csv
def make_hist(csv_filepath):
data = open(csv_filepath)
reader = csv.reader(data)
column = []
for row in data:
column.append(row)
plt.hist(column)
plt.title("Number of Backers")
plt.xlabel("Frequency")
plt.ylabel("Value")
plt.show()
# fig = plt.gcf()
# plot_url = py.plot_mpl(fig, filename='backers_histogram')
backer_data = r"C:/Users/user/Documents/user/Programming/count_backers.csv"
make_hist(backer_data)
我开始使用Learn Python the Hard Way学习python,我习惯于从PowerShell执行python文件。但是,我很难在Windows上安装和构建matplotlib,并且为了时间的缘故,我决定将它安装在Linux子系统上用于Linux Ubuntu。在我能够正常工作后,我现在收到以下错误:
Traceback (most recent call last):
File "histogram.py", line 22, in <module>
make_hist(backer_data)
File "histogram.py", line 7, in make_hist
data = open(csv_filepath)
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/user/Documents/user/Programming/count_backers.csv'
我尝试了几种不同版本的文件路径,包括:
backer_data = r"C:\Users\user\Documents\user\Programming\count_backers.csv"
backer_data = "mnt/c/Users/user/Documents/user/Programming/count_backers.csv"
backer_data = r"mnt\c\Users\user\Documents\user\Programming\count_backers.csv"
分别产生以下错误:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\user\\Documents\\user\\Programming\\count_backers.csv'
FileNotFoundError: [Errno 2] No such file or directory: 'mnt/c/Users/user/Documents/user/Programming/count_backers.csv'
FileNotFoundError: [Errno 2] No such file or directory: 'mnt\\c\\Users\\user\\Documents\\user\\Programming\\count_backers.csv'
当我在指定的目录上使用ls
时虽然它确实显示了文件在那里:
smidem@JealousHippo:/mnt/c/Users/user/Documents/user/Programming$ ls
column_avg.py count_backers.csv histogram.py pledged.csv
我很困惑为什么它无法找到这个文件,任何帮助将不胜感激。
在l0b0的帮助下,我将backer_data
更改为backer_data = "/mnt/c/Users/user/Documents/user/Programming/count_backers.csv"
,但我现在收到此回复:
Traceback (most recent call last):
File "histogram.py", line 22, in <module>
make_hist(backer_data)
File "histogram.py", line 12, in make_hist
plt.hist(column)
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 2947, in hist
ax = gca()
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 928, in gca
return gcf().gca(**kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 578, in gcf
return figure()
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 527, in figure
**kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 84, in new_figure_manager
return new_figure_manager_given_figure(num, figure)
File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 92, in new_figure_manager_given_figure
window = Tk.Tk()
File "/usr/lib/python3.5/tkinter/__init__.py", line 1871, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
答案 0 :(得分:0)
"mnt/c/Users/user/Documents/user/Programming/count_backers.csv"
是从当前工作目录开始的相对路径,很可能不是文件系统根目录。尝试用斜杠开始路径。