2017-01-24 21 views
0

価格データでトレンドラインを正しくプロットすることができますが、日付フォーマットのX Y軸は両方とも空白です。私は軸のためにこのプロット構成を台無しにしているのか分かりません。トレンドラインで日付のPython Matplotlib軸が空白

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']はの値サンプルなければなりません:ここではPython 2.7のコードである

114.684 
114.679 

df['Time']はなりサンプルの値が含まれます。

23:20 
23:21 
+0

をあなたの 'ax.plot(X、P(x)は、R ' - ')に移動した場合に'ステートメントをあなたの問題を解決するかもしれない 'ax = fig.add_subplot(111)'のすぐ下にあります。 – Chuck

答えて

1

アップデート:私はのソースを発見しましたあなたの問題。

以下の問題に加えて、リンクされた質問への回答が間違ってコピーされました。

あなたが書いた: ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%3.4f')) あなたが必要です: ax.yaxis.set_major_formatter(FormatStrFormatter('%3.4f'))

更新グラフを参照してください:あなたが実際に何かをプロットしている前に、あなたのコードで

https://imgur.com/a/RvO4z

は、あなたは軸の変更を開始します。

あなたは自分のadd_subplot線の下にこれが動作するためにあなたのax.plot(x, p(x), '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() 
関連する問題