当前位置: 首页 > 工具软件 > OpenP2M > 使用案例 >

python date2num_坚持mdates.date2num

怀展
2023-12-01

我一直在尝试寻找使用mdates.date2num的解决方案,但是尽管尝试了一些方法,我还是有错误。在

这是我的代码:import matplotlib.pyplot as plt

import numpy as np

import urllib

import matplotlib.dates as mdates

#from matplotlib.dates import bytespdate2num

'''

datefunc = lambda x: mdates.date2num(datetime.strptime(x, '%Y%m%d'))

'''

def graph_data(stock):

fig = plt.figure()

ax1 = plt.subplot2grid((1,1), (0,0),)

stock_price_url =('http://chartapi.finance.yahoo.com/instrument/1.0/+stock+/chartdata;type=quote;range=10y/csv')

source_code = urllib.request.urlopen(stock_price_url).read().decode()

stock_data = []

spilt_source = source_code.split('\n')

for line in source_code:

spilt_line = line.split(',')

if len(spilt_line) == 6:

if 'values' not in line and 'labels' not in line:

stock_data.append(line)

date, closep, highp, lowp, openp, volume = np.loadtxt(

stock_data,

delimiter=',',

unpack=True,

# %Y = Full year. 2015

# %y = partial year 15

# %m = number of month

# %d = number day

# %H = hours

# %M = minutes

# %S = seconds

converters={0: mdates.date2num('%Y%m%d')})

#converters={0: datefunc})

ax1.plot_date(date, closep, '-', label='Price')

for label in ax1.xaxis.get_ticklables():

label.set_rotation(45)

plt.xlabel('date')

plt.ylabel('Price')

plt.title('Interesting Graph')

plt.legend()

plt.show()

graph_data('TSLA')

正如评论所示,我已经尝试了几件事,但都没有成功。

我正在使用python3和pyCharm。在

 类似资料: