2017-08-07 78 views
0

this例: を使用しています。次のコードを使用してグリッドを作成し、最初の日付(csvファイルの最初の日付それで、例えば3月7日ではなく7月7日)。すべての日付が消えるの上に、私はコードを書くと、グリッドは、x軸をマーキング何行表示されないときmatplotlibで週末の空白をプロットせずに日付を表​​示する方法

months = matplotlib.dates.MonthLocator() 
ax.xaxis.set_major_locator(months) 

しかし(以下のコード2を参照してください):

enter image description here

がどのように私はset_major_locator()を作ることができます月の最初の日を見つける(最初にcsvファイルから入手できます)?絵で 私はここでの問題

ax.plot(r.date, r.adj_close, 'o-') 
months = matplotlib.dates.MonthLocator() 
ax.xaxis.set_major_locator(months) 

enter image description here

を使用してグリッドは、月の最初の日を見つける作ったの下には、空の日にスペースを作り、これは私がしようとしている問題になるということですグラフ上のプロット線。

編集: コード1は、(適切な場所にグリッドを置きますが、私はそれが週末などにスペースを作るようラインをプロットしてみたときにトラブルになります):されません)(

def plot1(price, date): 
    # first we'll do it the default way, with gaps on weekends 
    fig, ax = plt.subplots() 
    ax.plot(date, price, 'o-',c='black', markersize=2.7, linewidth=0.9) 
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%Y-%d')) 
    days_locator = mdates.DayLocator(bymonthday=[1,]) 
    ax.xaxis.set_major_locator(days_locator) 
    ax.set_title("Default") 
    fig.autofmt_xdate() 
    ax.grid() 
    plt.show() 

コード2(set_major_locator作業):

def plot2(price, date): 
    # next we'll write a custom formatter 
    N = len(price) 
    ind = np.arange(N) # the evenly spaced plot indices 
    def format_date(x, pos=None): 
     thisind = np.clip(int(x + 0.5), 0, N - 1) 
     return date[thisind].strftime('%Y-%m-%d') 
    fig, ax = plt.subplots() 
    ax.plot(ind, price, 'o-',c='black', markersize=2.7, linewidth=0.9) 
    ax.xaxis.set_major_formatter(mticker.FuncFormatter(format_date)) 
    ax.set_title("Custom tick formatter") 
    days_locator = mdates.DayLocator(bymonthday=[1,]) 
    ax.xaxis.set_major_locator(days_locator) 
    fig.autofmt_xdate() 
    ax.grid() 
    plt.show() 

答えて

0

はヶ月の最初の日を見つけるには、使用する必要があります。

days_locator = mdates.DayLocator(bymonthday=[1,]) 
ax.xaxis.set_major_locator(days_locator) 
+0

それは動作しませんplot2(私は最初の投稿の最後に2つのプロットのスクリプトを追加しました) – Kristin

+0

コードにはさまざまな問題があります。その例をhttps://matplotlib.org/examples/api/date_demo.htmlで見ることをお勧めします – axaroth

関連する問題