2016-08-05 3 views
1

matplotlibのグラフ内にグラフが作成された日付を示すテキストボックスを作成しようとしています。figtext datetime function matplotlib

matplotlibのfigtext関数を使用してグラフの右下にテキストボックスを作成しましたが、日付を表示するようにテキストボックス内にpython datetime関数を組み込む方法がわかりません。何か案は?

コードやグラフの下:

#Stacked Bar Char- matplotlib 
#Create the general blog and the "subplots" i.e. the bars 
f, ax1 = plt.subplots(1, figsize=(8,5), dpi=1000) 
# Set the bar width 
bar_width = 0.50 
# positions of the left bar-boundaries 
bar_l = [i+1 for i in range(len(df4['Pandas']))] 
# positions of the x-axis ticks (center of the bars as bar labels) 
tick_pos = [i+(bar_width/2) for i in bar_l] 

#Stack the negative bars by region 
#Start bottom at 0, and then use pandas to add bars together 
ax1.bar(bar_l, df4['cats1'], width=bar_width, label='cats',color='R', alpha=.4, align='center', 
     bottom = 0) 
ax1.bar(bar_l, df4['dogs1'], width=bar_width,label='dogs',color='#ADD8E6',alpha=.4, fill=True, align='center', 
     bottom =(df4['cats1'])) 
ax1.bar(bar_l, df4['zebras1'], width=bar_width, label='zebras',color='#FFA500',alpha=.4, align='center', 
     bottom = np.array(df4['cats1'])+np.array(df4['dogs1'])) 
ax1.bar(bar_l, df4['pandas1'], width=bar_width, label='pandas', color='b',alpha=.5, fill=True, align='center', 
     bottom = np.array(df4['cats1'])+np.array(df4['dogs1'])+np.array(df4['zebras1'])) 
#Stack the positive bars by region 
#Start bottom at 0, and then use pandas to add bars together 
ax1.bar(bar_l, df4['cats'], width=bar_width,color='R', alpha=.4, align='center', 
     bottom = 0) 
ax1.bar(bar_l, df4['dogs'], width=bar_width,color='#ADD8E6',alpha=.4, fill=True, align='center', 
     bottom =(df4['cats'])) 
ax1.bar(bar_l, df4['zebras'], width=bar_width ,color='#FFA500',alpha=.4, align='center', 
     bottom = np.array(df4['cats'])+np.array(df4['dogs'])) 
ax1.bar(bar_l, df4['pandas'], width=bar_width, color='b',alpha=.5, fill=True, align='center', 
     bottom = np.array(df4['cats'])+np.array(df4['dogs'])+np.array(df4['zebras'])) 

# set the x ticks with names 
plt.xticks(tick_pos, df4['Year'],fontsize=10) 


# Set the label and legends 
plt.title('Animals on the farm', fontweight='bold') 
ax1.set_ylim([-1600,1000]) 
ax1.set_ylabel("Count",fontsize=12) 
ax1.set_xlabel("Year",fontsize=12) 
plt.legend(loc='upper left', prop={'size':6}) 
ax1.axhline(y=0, color='k') 
ax1.axvline(x=0, color='k') 
plt.figtext(0, 0,"Data as of ", wrap=False, 
      horizontalalignment='left',verticalalignment ='bottom', fontsize=8) 
plt.setp(ax1.get_yticklabels(), rotation='horizontal', fontsize=10) 
plt.show() 

Graph= Animals on the Farm

答えて

0

ここで今日の日付を持つ例を示します

import datetime as dt 

plt.figtext(0, 0,"Data as of {}".format(dt.date.today().strftime('%Y-%m-%d')), wrap=False, 
      horizontalalignment='left',verticalalignment ='bottom', fontsize=8) 
0

あなたがdatetimeオブジェクトを持っている場合は、変換することstrftimeメソッドを使用することができますあなたが望むフォーマットを使用して文字列表現に変換します:

from datetime import datetime 
plt.figtext(0, 0, 'Date as of ' + datetime.now().strftime('%Y-%m-%d')) 
+0

ありがとうございます!これは非常に便利でした。 – spacedinosaur10