Pandas Plots:周末的单独颜色,x 轴上漂亮的打印时

时间:2023-03-24
本文介绍了Pandas Plots:周末的单独颜色,x 轴上漂亮的打印时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类似于

我有几个问题:

  1. 我怎样才能特别显示周末.我曾想过的一些方法是获取与周末相对应的索引,然后在 xlims 之间绘制透明条.也可以绘制相同的矩形.最好能在 Pandas 中简单地完成.
  2. 日期格式不是最漂亮的

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

Following is the code used to generate this plot

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)])

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

The ax.bar is specifically for highlighting weekends, but it does not produce any visible output. (Problem 1) For Problem 2 i tried to use Major Formatter and Locators, the code is as follows:

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);

产生的输出如下:

了解 Dataframe 的样子可能会有所帮助

It may be helpful to know what the Dataframe looks like

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)

推荐答案

我尝试了很多,现在这些技巧有效.等待更 Pythonic 和一致的解决方案.标注问题的解决方案:

I tried a lot and for now these hacks work. Await a more Pythonic and consistent solutions. Solution to labeling problems:

def correct_labels(ax):
    labels = [item.get_text() for item in ax.get_xticklabels()]
    days=[label.split(" ")[0] for label in labels]
    months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
    final_labels=[]
    for i in range(len(days)):
        a=days[i].split("-")
        final_labels.append(a[2]+"
"+months[int(a[1])-1])
    ax.set_xticklabels(final_labels)

另外,在绘图时,我进行了以下更改

Also while plotting i make the following change

ax=df.plot(kind='bar',rot=0)

这会使标签旋转为 0.

This makes the labels at 0 rotation.

为了找到周末并突出显示它们,我编写了以下两个函数:

For finding weekends and highlighting them, i wrote the following two functions:

def find_weekend_indices(datetime_array):
    indices=[]
    for i in range(len(datetime_array)):
        if datetime_array[i].weekday()>=5:
            indices.append(i)
    return indices

def highlight_weekend(weekend_indices,ax):
    i=0
    while i<len(weekend_indices):
         ax.axvspan(weekend_indices[i], weekend_indices[i]+2, facecolor='green', edgecolor='none', alpha=.2)
         i+=2

现在,该图看起来更加有用,并且涵盖了这些用例.

Now, the plot looks much more useful and covers these use cases.

这篇关于Pandas Plots:周末的单独颜色,x 轴上漂亮的打印时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:将 Python 序列(时间序列/数组)拆分为重叠的子序列 下一篇:如何使用 pandas 按 10 分钟对时间序列进行分组

相关文章

最新文章