2016-05-31 19 views
3

は、私は、日時によってインデックス付け、日付ごとにグループ化されている観測(ベビーボトルの供給量)のデータフレームを持っている:プロット大目盛とラベル

... 
bottles = bottles.set_index('datetime') 
bottles = bottles.groupby(bottles.index.date) 

I

ax = plt.gca() 
ax.xaxis.set_major_locator(mdates.DayLocator()) 
ax.xaxis.set_minor_locator(mdates.HourLocator()) 
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y')) 
bottles['volume'].cumsum().plot(kind='bar', figsize=[16,8]) 
ax.xaxis.grid(True, which="major") 
ax.xaxis.grid(False, which="minor") 
ax.yaxis.grid(True) 
plt.gcf().autofmt_xdate() 
plt.show() 

生成します:plot

0を、それは深夜に毎日とリセットを増加するつまり、栄養の量を示して - 彼らは毎日を増やすよう累積値をプロットするためにmatplotlibのを使用したいです

私は1日に一度x軸の日付にラベルを付けるだけで、日付境界(24時間ごと)には垂直グリッド線を描画したいと思います。どのように上記のコードを修正するための任意の推奨事項?

+0

パンダバープロットがカテゴリプロットであることを前提として、あなたが常に表示、すべてのバーのための目盛りを持っているつもりです。私の推測では、あなたはpandasのインターフェースではなくmatplotlibオブジェクトに対して直接プロットを書く必要があります。 –

答えて

0

データを入力していないため、ダミーデータが生成されました。本質的には、x軸上のティックを取り出してから、時間軸のティックラベルを表示させることで、ラベルを見えなくすることができます。

注:これは何時間も使用できますので、必要に応じてresampleのデータフレームを時間単位で処理してください。

import random 
import pandas 
import matplotlib.pyplot as plt 

#generate dummy data and df 
dates = pd.date_range('2017-01-01', '2017-01-10', freq='H') 
df = pd.DataFrame(np.random.randint(0, 10, size=(1, len(dates)))[0], index=dates) 
ax = df.groupby(pd.TimeGrouper('D')).cumsum().plot(kind='bar', width=1, align='edge', figsize=[16,8]) #cumsum with daily reset. 
ax.xaxis.grid(True, which="major") 
#ax.set_axisbelow(True) 

#set x-labels to certain date format 
ticklabels = [i.strftime('%D') for i in df.index] 
ax.set_xticklabels(ticklabels) 

#only show labels once per day (at the start of the day) 
xticks = ax.xaxis.get_major_ticks() 
n=24 # every 24 hours 
for index, label in enumerate(ax.get_xaxis().get_ticklabels()): 
    if index % n != 0: 
     label.set_visible(False) # hide labels 
     xticks[index].set_visible(False) # hide ticks where labels are hidden 

ax.legend_.remove() 
plt.show() 

結果: Result

関連する問題