timeseries.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:coquery 作者: gkunter 项目源码 文件源码
def draw(self, **kwargs):
        """ Draw time series. """

        def plot_facet(data, color, **kwargs):
            num = []
            date = []
            time = data[self._time_column]
            num = data[self._time_column].apply(self.convert_to_datetime)
            date = data[self._time_column].apply(self.convert_to_timeseries)
            if pd.isnull(num).sum() <= pd.isnull(date).sum():
                data[self._time_column] = num
            else:
                data[self._time_column] = date

            data.dropna(inplace=True)
            if len(self._groupby) == 2:
                ct = pd.crosstab(data[self._time_column], data[self._groupby[0]])
                ct = ct.reindex_axis(self._levels[0], axis=1).fillna(0)
                ct = ct[pd.notnull(ct.index)]
            else:
                ct = pd.crosstab(
                    data[self._time_column],
                    pd.Series([""] * len(self._table[self._time_column]), name=""))

            # percentage area plot:
            if self.percentage:
                # if there is only one grouping variable (the time column), 
                # the cross table produces a Series, not a data frame. It 
                # isn't really very informative to plot it, but we provide 
                # for this special case anyway_
                if type(ct) == pd.Series:
                    ct = ct.apply(lambda x: 100)
                else:
                    ct = ct.apply(lambda x: (100 * x) / sum(x), axis=1)
                ct.plot(kind="area", ax=plt.gca(), stacked=True, color=self.get_palette(), **kwargs)
            else:
                if self.area:
                    # Stacked area plot:
                    if len(self._groupby) == 2:
                        self.vmax = max(self.vmax, ct.apply(sum, axis=1).max())
                    ct.plot(ax=plt.gca(), kind="area", stacked=True, color=self.get_palette(), **kwargs)
                else:
                    # Line plot:
                    self.vmax = max(self.vmax, ct.values.max())
                    ct.plot(ax=plt.gca(), color=self.get_palette())

        self.map_data(plot_facet)

        if self.percentage:
            self.g.set(ylim=(0, 100))
        else:
            self.g.set(ylim=(0, self.vmax))
        self.g.set_axis_labels(self.options["label_x_axis"], self.options["label_y_axis"])

        if len(self._groupby) == 2:
            self.add_legend()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号