使用具有不同xlabel的 pandas 绘制日期时间条形图 [英] Plotting a Datetime Bar Graph with Pandas with different xlabels

查看:75
本文介绍了使用具有不同xlabel的 pandas 绘制日期时间条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个条形图,该条形图在带有条形图的pandas DataFrame的每一列中仅包含少量数据.这是成功的,但它不仅具有错误的y轴限制,而且还使x刻度线间隔非常近,因此该图无用.我想将步速更改为大约每周左右,并且仅显示日,月和年.我有以下DataFrame:

I would like to plot a bar graph that has only a few entries of data in each column of a pandas DataFrame with a bar graph. This is successful, but not only does it have the wrong y-axis limits, it also makes the x ticks very closely spaced so that the graph is useless. I would like to change the step rate to be about every week or so and only display day, month and year. I have the following DataFrame:

       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

以及以下代码进行绘制:

And the following code to plot it:

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轴以小时为单位,这是我将数据分开的时间,因此这很有意义.但是,这不是有用的显示.

The y-axis starts at 0, but continues to about double the maximum value of the y-limit. The x-axis counts by hours, which is what I separated the data by, so that makes sense. However, it is not a helpful display.

推荐答案

看下面的代码:

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的 pandas 绘制日期时间条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆