Python 使用带有不同xlabel的Pandas绘制日期时间条形图

Python 使用带有不同xlabel的Pandas绘制日期时间条形图,python,python-2.7,pandas,matplotlib,Python,Python 2.7,Pandas,Matplotlib,我想绘制一个条形图,在带有条形图的pandas DataFrame的每列中只有几个数据条目。这是成功的,但它不仅有错误的y轴限制,它还使x记号间隔非常近,因此图形是无用的。我想将步进速率更改为每周左右,仅显示日、月和年。我有以下数据帧: Observed WRF 2014-06-28 12:00:00 0.0 0.0 2014-06-28 13:00:00 0.0 0.0 2014-06-28 14:00:00 0.0 0.0 2014-

我想绘制一个条形图,在带有条形图的pandas DataFrame的每列中只有几个数据条目。这是成功的,但它不仅有错误的y轴限制,它还使x记号间隔非常近,因此图形是无用的。我想将步进速率更改为每周左右,仅显示日、月和年。我有以下数据帧:

       Observed  WRF
2014-06-28 12:00:00       0.0  0.0
2014-06-28 13:00:00       0.0  0.0
2014-06-28 14:00:00       0.0  0.0
2014-06-28 15:00:00       0.0  0.0
2014-06-28 16:00:00       0.0  0.0
2014-06-28 17:00:00       0.0  0.0
2014-06-28 18:00:00       0.0  0.0
2014-06-28 19:00:00       0.0  0.0
2014-06-28 20:00:00       0.0  0.0
2014-06-28 21:00:00       0.0  0.0
2014-06-28 22:00:00       0.0  0.0
2014-06-28 23:00:00       0.0  0.0
2014-06-29 00:00:00       0.0  0.0
2014-06-29 01:00:00       0.0  0.0
2014-06-29 02:00:00       0.0  0.0
2014-06-29 03:00:00       0.0  0.0
2014-06-29 04:00:00       0.0  0.0
2014-06-29 05:00:00       0.0  0.0
2014-06-29 06:00:00       0.0  0.0
2014-06-29 07:00:00       0.0  0.0
2014-06-29 08:00:00       0.0  0.0
2014-06-29 09:00:00       0.0  0.0
2014-06-29 10:00:00       0.0  0.0
2014-06-29 11:00:00       0.0  0.0
2014-06-29 12:00:00       0.0  0.0
2014-06-29 13:00:00       0.0  0.0
2014-06-29 14:00:00       0.0  0.0
2014-06-29 15:00:00       0.0  0.0
2014-06-29 16:00:00       0.0  0.0
2014-06-29 17:00:00       0.0  0.0
                      ...  ...
2014-07-04 02:00:00       0.0002    0.0
2014-07-04 03:00:00       0.2466    0.0
2014-07-04 04:00:00       0.7103    0.0
2014-07-04 05:00:00       0.9158    1.93521e-13
2014-07-04 06:00:00       0.6583    0.0
2014-07-04 07:00:00       0.3915    0.0
2014-07-04 08:00:00       0.1249    0.0
2014-07-04 09:00:00       0.0       0.0
                      ...  ...
2014-08-30 07:00:00       0.0  0.0
2014-08-30 08:00:00       0.0  0.0
2014-08-30 09:00:00       0.0  0.0
2014-08-30 10:00:00       0.0  0.0
2014-08-30 11:00:00       0.0  0.0
2014-08-30 12:00:00       0.0  0.0
2014-08-30 13:00:00       0.0  0.0
2014-08-30 14:00:00       0.0  0.0
2014-08-30 15:00:00       0.0  0.0
2014-08-30 16:00:00       0.0  0.0
2014-08-30 17:00:00       0.0  0.0
2014-08-30 18:00:00       0.0  0.0
2014-08-30 19:00:00       0.0  0.0
2014-08-30 20:00:00       0.0  0.0
2014-08-30 21:00:00       0.0  0.0
2014-08-30 22:00:00       0.0  0.0
2014-08-30 23:00:00       0.0  0.0
2014-08-31 00:00:00       0.0  0.0
2014-08-31 01:00:00       0.0  0.0
2014-08-31 02:00:00       0.0  0.0
2014-08-31 03:00:00       0.0  0.0
2014-08-31 04:00:00       0.0  0.0
2014-08-31 05:00:00       0.0  0.0
2014-08-31 06:00:00       0.0  0.0
2014-08-31 07:00:00       0.0  0.0
2014-08-31 08:00:00       0.0  0.0
2014-08-31 09:00:00       0.0  0.0
2014-08-31 10:00:00       0.0  0.0
2014-08-31 11:00:00       0.0  0.0
2014-08-31 12:00:00       0.0  0.0
和下面的代码来绘制它:

df4.plot(kind='bar',edgecolor='none',figsize=(16,8),linewidth=2, color=((1,0.502,0),'black'))
plt.legend(prop={'size':16})
plt.subplots_adjust(left=.1, right=0.9, top=0.9, bottom=.1)
plt.title('Five Day WRF Model Comparison Near %.2f,%.2f' %(lat,lon),fontsize=24)
plt.ylabel('Hourly Accumulated Precipitation [mm]',fontsize=18,color='black')
ax4=plt.gca()
maxs4=df4.max()
ax4.set_ylim([0, maxs4.max()])
ax4.xaxis_date()
ax4.xaxis.set_label_coords(0.5, -0.05) 
plt.xlabel('Time',fontsize=18,color='black')
plt.show()
y轴从0开始,但继续大约是y限制最大值的两倍。x轴以小时为单位计数,这就是我用来分隔数据的方式,所以这是有意义的。但是,这不是一个有用的显示。

请查看以下代码:

import pandas as pd
import numpy as np
from datetime import datetime 
import matplotlib.pylab as plt
from matplotlib.dates import DateFormatter

# Sample data
df_origin = pd.DataFrame(pd.date_range(datetime(2014,6,28,12,0,0), 
 datetime(2014,8,30,12,0,0), freq='1H'), columns=['Valid Time'])
df_origin = df_origin .set_index('Valid Time')
df_origin ['Precipitation'] = np.random.uniform(low=0., high=10., size=(len(df_origin.index)))
df_origin .loc[20:100, 'Precipitation'] = 0.
df_origin .loc[168:168*2, 'Precipitation'] = 0. # second week has to be dry

# Plotting
df_origin.plot(y='Precipitation',kind='bar',edgecolor='none',figsize=(16,8),linewidth=2, color=((1,0.502,0)))
plt.legend(prop={'size':16})
plt.subplots_adjust(left=.1, right=0.9, top=0.9, bottom=.1)
plt.title('Precipitation (WRF Model)',fontsize=24)
plt.ylabel('Hourly Accumulated Precipitation [mm]',fontsize=18,color='black')
ax = plt.gca()
plt.gcf().autofmt_xdate()

# skip ticks for X axis
ax.set_xticklabels([dt.strftime('%Y-%m-%d') for dt in df_origin.index])
for i, tick in enumerate(ax.xaxis.get_major_ticks()):
    if (i % (24*7) != 0): # 24 hours * 7 days = 1 week
        tick.set_visible(False)

plt.xlabel('Time',fontsize=18,color='black')

plt.show()

所以,我想这更像是一个标签问题。我很抱歉没有那么清楚。我希望简单地将XLabel更改为每周,而不是每小时,同时仍然保持相同的垃圾箱。所以,数据将按小时分组,但xlabel不会。是的,您的问题不太清楚。你想要的是更简单,我对上面的代码做了修改。谢谢。看起来很不错。