Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib轴对于带有趋势线的日期为空_Python_Matplotlib_Trendline - Fatal编程技术网

Python Matplotlib轴对于带有趋势线的日期为空

Python Matplotlib轴对于带有趋势线的日期为空,python,matplotlib,trendline,Python,Matplotlib,Trendline,我可以用价格数据正确地绘制趋势线,但日期格式的X轴和Y轴均为空。我不确定是什么弄乱了轴的绘图配置。以下是Python 2.7代码: y = df['Close'] # calc the trendline http://stackoverflow.com/questions/26447191/how-to-add-trendline-in-python-matplotlib-dot-scatter-graphs l = [] for t in df['Time']: datetime

我可以用价格数据正确地绘制趋势线,但日期格式的X轴和Y轴均为空。我不确定是什么弄乱了轴的绘图配置。以下是Python 2.7代码:

y = df['Close']

# calc the trendline http://stackoverflow.com/questions/26447191/how-to-add-trendline-in-python-matplotlib-dot-scatter-graphs

l = []
for t in df['Time']:
    datetime_object = datetime.datetime.strptime(str(t), '%H:%M')
    print datetime_object.hour
    print datetime_object.minute
    l.append((3600 * datetime_object.hour + 60 * datetime_object.minute))
x = l
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
fig = plt.figure()
ax = fig.add_subplot(111)

#http://stackoverflow.com/questions/17709823/plotting-timestamps-hour-minute-seconds-with-matplotlib
plt.xticks(rotation=25)
ax = plt.gca()
ax.set_xticks(x)
xfmt = md.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(xfmt)

ax.plot(x, p(x), 'r--')
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%3.4f')) #http://stackoverflow.com/questions/29188757/matplotlib-specify-format-of-floats-for-tick-lables
plt.show()
此外,df['Close']的值样本为:

114.684
114.679
df['Time']将包含样本值:

23:20
23:21

更新:我找到了你问题的根源

除了下面的问题,您还错误地复制了链接问题的答案

您编写了:ax.yaxis.set\u major\u formattermtick.formatterformatter“%3.4f” 您需要:ax.yaxis.set\u major\u formatterFormatterFormatterFormatter“%3.4f”

请参见更新的图表:

在代码中,在实际绘制任何图形之前,就开始轴更改

如果将ax.plotx、px、“r-”移动到添加子地块线的正下方,这将起作用:

import numpy as np
from matplotlib import pyplot as plt
import datetime

import matplotlib
from matplotlib.ticker import FormatStrFormatter

df = pandas.DataFrame()
df['Time'] = pandas.Series(['23:2','22:1'])
df['Close'] = pandas.Series([114.,114.])

y = df['Close']

# calc the trendline http://stackoverflow.com/questions/26447191/how-to-add-trendline-in-python-matplotlib-dot-scatter-graphs

l = []
for t in df['Time']:
    datetime_object = datetime.datetime.strptime(str(t), '%H:%M')
    print datetime_object.hour
    print datetime_object.minute
    l.append((3600 * datetime_object.hour + 60 * datetime_object.minute))
x = l
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
fig = plt.figure()
ax = fig.add_subplot(111)
#Added:
ax.plot(x, p(x), 'r--')

#http://stackoverflow.com/questions/17709823/plotting-timestamps-hour-   minute-seconds-with-matplotlib
plt.xticks(rotation=25)
ax = plt.gca()
ax.set_xticks(x)
xfmt = md.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(xfmt)

# REMOVED: ax.plot(x, p(x), 'r--')
# Changed: ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%3.4f')) 
ax.yaxis.set_major_formatter(FormatStrFormatter('%3.4f'))
#http://stackoverflow.com/questions/29188757/matplotlib-specify-format-of-    floats-for-tick-lables
plt.show()
如果将ax.plotx,px,'r-'语句移动到ax=fig.add_子图111的正下方,这可能会解决您的问题。