Anaconda python matplotlib函數庫找不到文件sample_data\\goog.npz類似問題解決方案
問題描述:
在運行matplotlib官網示例代碼https://matplotlib.org/gallery/lines_bars_and_markers/scatter_demo2.html#sphx-glr-gallery-lines-bars-and-markers-scatter-demo2-py
代碼如下:
import numpy as np import matplotlib.pyplot as plt import matplotlib.cbook as cbook # Load a numpy record array from yahoo csv data with fields date, open, close, # volume, adj_close from the mpl-data/example directory. The record array # stores the date as an np.datetime64 with a day unit ('D') in the date column. with cbook.get_sample_data('goog.npz') as datafile: price_data = np.load(datafile)['price_data'].view(np.recarray) price_data = price_data[-250:] # get the most recent 250 trading days delta1 = np.diff(price_data.adj_close) / price_data.adj_close[:-1] # Marker size in units of points^2 volume = (15 * price_data.volume[:-2] / price_data.volume[0])**2 close = 0.003 * price_data.close[:-2] / 0.003 * price_data.open[:-2] fig, ax = plt.subplots() ax.scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.5) ax.set_xlabel(r'$\Delta_i$', fontsize=15) ax.set_ylabel(r'$\Delta_{i+1}$', fontsize=15) ax.set_title('Volume and percent change') ax.grid(True) fig.tight_layout() plt.show()
過程中產生報錯信息如下
runfile('D:/python_matplotlib_charts/散點演示2.py', wdir='D:/python_matplotlib_charts') Traceback (most recent call last): File "D:\python_matplotlib_charts\散點演示2.py", line 15, in <module> with cbook.get_sample_data('goog.npz') as datafile: File "C:\Users\18136\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 490, in get_sample_data return open(path, mode) FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\18136\\Anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\sample_data\\goog.npz'
報錯信息翻譯過來就是:找不到'C:\\Users\\18136\\Anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\sample_data路徑下的goog.npz
經過檢索該路徑發現matplotlib\\mpl_data下沒有sample_data文件夾
解決方案:
Windows下:
1.打開anaconda prompt 輸入以下命令並執行:
conda install mpl_sample_data
2.再次打開路徑C:\\Users\\18136\\Anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\,創建一個文件夾命名為“sample_data“ 再將mpl_data路徑下所有非文件夾文件,注意:非文件夾文件!(如下這些)移動到我們創建好的sample_data文件夾下。問題解決。
Linux下:
只需執行命令:
conda install mpl_sample_data
無需移動文件。問題解決。
遇到問題歡迎提問。