【问题标题】:Python Matplotlib axis is blank for date with trend linePython Matplotlib 轴对于带有趋势线的日期为空白
【发布时间】:2017-01-24 20:07:47
【问题描述】:

我可以正确绘制带有价格数据的趋势线,但日期格式的 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.plot(x, p(x), 'r--') 语句移动到ax = fig.add_subplot(111) 下方,这可能会解决您的问题。

标签: python matplotlib trendline


【解决方案1】:

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

除了以下问题之外,您错误地复制了链接问题的答案。

您写道:ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%3.4f')) 你需要:ax.yaxis.set_major_formatter(FormatStrFormatter('%3.4f'))

查看更新的图表:

https://imgur.com/a/RvO4z

在您的代码中,您在实际绘制任何内容之前开始更改轴。

如果您将ax.plot(x, p(x), 'r--') 移动到您的add_subplot 行下方,这将起作用:

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 2018-07-18
    • 2020-11-28
    • 2019-03-19
    相关资源
    最近更新 更多