简体   繁体   English

尝试使用 fig、ax 或 plt.plot 时出现问题

[英]Issue when trying to use fig, ax or plt.plot

My following code is:我的以下代码是:

import matplotlib
matplotlib.use('Agg') #necessario para salvar figura no crontab
import os
import numpy as np
import matplotlib.pylab as plt
from pandas import Series, DataFrame
import pandas as pd
import datetime as dt
from datetime import datetime
from pylab import *
import matplotlib.dates as mdates


pathname = 'C:\\Users\\leo_v\\Desktop\\Nova_pasta\\leo_170818\lioc\\' 
pathnamep = 'C:\\Users\\leo_v\\Desktop\\Nova_pasta\\leo_170818\\avalia\\' 

prevs = np.sort(os.listdir(pathnamep))


rgp1 = pd.read_csv(pathnamep + prevs[-3] + '/PNBOIA_riogrande.txt',sep='\s*',
                     names=['ano','mes','dia','hora','minu','hs','tp','dp','spr'])

flp1 = pd.read_csv(pathnamep + prevs[-3] + '/PNBOIA_floripa.txt',sep='\s*',
                     names=['ano','mes','dia','hora','minu','hs','tp','dp','spr'])

sap1 = pd.read_csv(pathnamep + prevs[-3] + '/PNBOIA_santos.txt',sep='\s*',
                     names=['ano','mes','dia','hora','minu','hs','tp','dp','spr'])

rjp1 = pd.read_csv(pathnamep + prevs[-3] + '/PNBOIA_SIODOC.txt',sep='\s*',
                     names=['ano','mes','dia','hora','minu','hs','tp','dp','spr'])


vip1 = pd.read_csv(pathnamep + prevs[-3] + '/PNBOIA_vitoria.txt',sep='\s*',
                     names=['ano','mes','dia','hora','minu','hs','tp','dp','spr'])

rgp1['data'] = pd.to_datetime(rgp1.ano.astype(str) + '/' + rgp1.mes.astype(str) + '/' + rgp1.dia.astype(str) + '-' + rgp1.hora.astype(str) ,format="%Y/%m/%d-%H")
flp1['data'] = pd.to_datetime(flp1.ano.astype(str) + '/' + flp1.mes.astype(str) + '/' + flp1.dia.astype(str) + '-' + flp1.hora.astype(str) ,format="%Y/%m/%d-%H")
sap1['data'] = pd.to_datetime(sap1.ano.astype(str) + '/' + sap1.mes.astype(str) + '/' + sap1.dia.astype(str) + '-' + sap1.hora.astype(str) ,format="%Y/%m/%d-%H")
rjp1['data'] = pd.to_datetime(rjp1.ano.astype(str) + '/' + rjp1.mes.astype(str) + '/' + rjp1.dia.astype(str) + '-' + rjp1.hora.astype(str) ,format="%Y/%m/%d-%H")
vip1['data'] = pd.to_datetime(vip1.ano.astype(str) + '/' + vip1.mes.astype(str) + '/' + vip1.dia.astype(str) + '-' + vip1.hora.astype(str) ,format="%Y/%m/%d-%H")

rgp1 = rgp1.set_index('data')
flp1 = flp1.set_index('data')
sap1 = sap1.set_index('data')
rjp1 = rjp1.set_index('data')
#izp1 = izp1.set_index('data')
vip1 = vip1.set_index('data')

rgp1 = rgp1.ix[:,['hs','tp','dp','spr']]
flp1 = flp1.ix[:,['hs','tp','dp','spr']]
sap1 = sap1.ix[:,['hs','tp','dp','spr']]
rjp1 = rjp1.ix[:,['hs','tp','dp','spr']]
#izp1 = izp1.ix[:,['hs','tp','dp']]
vip1 = vip1.ix[:,['hs','tp','dp','spr']]

hoje = dt.datetime.strftime(dt.datetime.today(),'%Y%m%d')

tj = 15 * 24

plt.rcParams['text.latex.preamble']=[r"\usepackage{lmodern}"]
params = {'text.usetex' : True,
'font.size' : 11,
'font.family' : 'lmodern',
'text.latex.unicode': True,
}
plt.rcParams.update(params) 



plt.rcParams['text.latex.preamble']=[r"\usepackage{lmodern}"]
params = {'text.usetex' : True,
'font.size' : 14,
'font.family' : 'lmodern',
'text.latex.unicode': True,
}
plt.rcParams.update(params) 


u=1*np.sin(np.radians(sap1.dp-180))
v=1*np.cos(np.radians(sap1.dp-180))


plt.plot(sap1.hs)

fig, ax = plt.subplots(figsize=(11,9))
surf = ax.quiver(sap1.index.astype(np.int64)[::3],sap1.hs[::3],u[::3],v[::3],sap1.tp[::3],zorder=1)
ax.quiver(sap1.index.astype(np.int64)[::3],sap1.hs[::3],u[::3],v[::3],edgecolor='k', facecolor='None', linewidth=.5)
ax.plot(sap1.index.astype(np.int64),sap1.hs,'gray',linewidth=3,zorder=0)
ax.grid(), ax.set_ylabel('Significant Wave Height (m)');ax.set_ylim([0,8])
cbar=fig.colorbar(surf,orientation='vertical')
cbar.set_label(label='Peak Period (s)',size=14)
cbar.ax.tick_params(labelsize=12)

The main objective of this code is obtain someting like this image (but the x axis should be dates instead days of week):这段代码的主要目标是获得像这张图片这样的东西(但 x 轴应该是日期而不是星期几):

在此处输入图片说明

For some reason that I can't understand when I try to use plt.plot or fig, ax returns me this error:出于某种原因,当我尝试使用 plt.plot 或 fig 时,我无法理解 ax 返回此错误:

FileNotFoundError: [WinError 2] The system cannot find the file specified: FileNotFoundError: [WinError 2] 系统找不到指定的文件:

The entire code is running well, all my variables are working.整个代码运行良好,我所有的变量都在工作。 The only problem is when I try to use specifically plt.plot() or fig, ax = plt.subplots(figsize=(11,9))唯一的问题是当我尝试专门使用plt.plot()fig, ax = plt.subplots(figsize=(11,9))

I'm using python 3.6 on Windows 10 machine.我在 Windows 10 机器上使用 python 3.6。

Would be thankful if someone could help.如果有人可以提供帮助,将不胜感激。

To me, the same problem arised.对我来说,同样的问题出现了。 The old-fashion restart of Python solved the problem. Python 的老式重启解决了这个问题。 Restarting the kernel was in my case not enough, but a full restart did the job (I use the Spyder platform, which I restarted).在我的情况下,重新启动内核是不够的,但是完全重新启动完成了这项工作(我使用的是我重新启动的 Spyder 平台)。

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

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