相关疑难解决方法(0)

Pandas Plots:周末分开的颜色,x轴上的漂亮印刷时间

我创建了一个看起来像的情节在此输入图像描述

我有几个问题:

  1. 我怎么能专门展示周末.我想到的一些方法是抓住对应周末的指数,然后在xlims之间绘制透明条.也可以绘制矩形.如果可以在熊猫中明白地完成它将是最好的.
  2. 日期格式不是最漂亮的

以下是用于生成此图的代码

ax4=df4.plot(kind='bar',stacked=True,title='Mains 1 Breakdown');
ax4.set_ylabel('Power (W)');
idx_weekend=df4.index[df4.index.dayofweek>=5]
ax.bar(idx_weekend.to_datetime(),[1800 for x in range(10)])
Run Code Online (Sandbox Code Playgroud)

ax.bar是专门用于突出周末,但它不会产生任何可见的输出.(问题1)对于问题2,我尝试使用Major Formatter和Locators,代码如下:

ax4=df4.plot(kind='bar',stacked=True,title='Mains 1 Breakdown');
ax4.set_ylabel('Power (W)');
formatter=matplotlib.dates.DateFormatter('%d-%b');
locator=matplotlib.dates.DayLocator(interval=1);
ax4.xaxis.set_major_formatter(formatter);
ax4.xaxis.set_major_locator(locator);
Run Code Online (Sandbox Code Playgroud)

产生的产出如下: 在此输入图像描述

了解Dataframe的外观可能会有所帮助

In [122]:df4

Out[122]:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 36 entries, 2011-04-19 00:00:00 to 2011-05-24 00:00:00
Data columns:
(0 to 6 AM) Dawn          19  non-null values
(12 to 6 PM) Dusk         19  non-null values
(6 to 12 Noon) Morning    19  non-null values
(6PM to 12 Noon) Night    20  non-null values
dtypes: float64(4)
Run Code Online (Sandbox Code Playgroud)

python time-series matplotlib pandas

9
推荐指数
2
解决办法
2982
查看次数

标签 统计

matplotlib ×1

pandas ×1

python ×1

time-series ×1