Beispiel #1
0
    def tick(
        self,
        locator: Locator | None = None,
        *,
        upto: int | None = None,
    ) -> Temporal:

        if locator is not None:
            # TODO accept tuple for major, minor?
            if not isinstance(locator, Locator):
                err = (f"Tick locator must be an instance of {Locator!r}, "
                       f"not {type(locator)!r}.")
                raise TypeError(err)
            major_locator = locator

        elif upto is not None:
            # TODO atleast for minticks?
            major_locator = AutoDateLocator(minticks=2, maxticks=upto)

        else:
            major_locator = AutoDateLocator(minticks=2, maxticks=6)

        self._major_locator = major_locator
        self._minor_locator = None

        self.format()

        return self
Beispiel #2
0
    def _get_locators(self, locator, upto):

        if locator is not None:
            major_locator = locator
        elif upto is not None:
            major_locator = AutoDateLocator(minticks=2, maxticks=upto)

        else:
            major_locator = AutoDateLocator(minticks=2, maxticks=6)
        minor_locator = None

        return major_locator, minor_locator
Beispiel #3
0
def myplot(txtName):
    # plot daily_temp_record
    df = pd.read_table(txtName, sep='[ |\t]', header=None, engine='python')
    columnNames = [
        'time', 'floor_id', 'room_id', 'set_tmp', 'real_tmp', 'real_var_open',
        'var_open'
    ]
    df.columns = columnNames[0:df.shape[1]]
    df['time'] = pd.to_datetime(df['time'], format='%d:%H:%M:%S')

    # 配置时间坐标轴
    plt.figure()
    plt.subplot(2, 1, 1)
    plt.plot_date(df['time'],
                  df['real_tmp'],
                  linestyle="-",
                  marker="None",
                  color='indigo')
    # 坐标轴,标题设置
    plt.xticks([])
    plt.ylabel('Temperature', size=10)
    plt.title(time.strftime("%H:%M"),
              size=10)  #plt.title('Temperature record',size=10)
    plt.gcf().autofmt_xdate()  # 自动旋转日期标记
    plt.grid(True)
    plt.axis("tight")
    plt.gca().xaxis.set_major_formatter(
        mdates.DateFormatter('%H:%M'))  # 显示时间坐标的格式
    autodates = AutoDateLocator()  # 时间间隔自动选取
    plt.gca().xaxis.set_major_locator(autodates)

    plt.subplot(2, 1, 2)
    plt.plot_date(df['time'],
                  df['real_var_open'],
                  linestyle="-",
                  marker="None",
                  color='firebrick')
    # 坐标轴,标题设置
    plt.xlabel('Time', size=10)
    plt.ylabel('var_open', size=10)
    plt.title('Var_open record', size=10)
    plt.gcf().autofmt_xdate()  # 自动旋转日期标记
    plt.grid(True)
    plt.axis("tight")
    plt.gca().xaxis.set_major_formatter(
        mdates.DateFormatter('%H:%M'))  # 显示时间坐标的格式
    autodates = AutoDateLocator()  # 时间间隔自动选取
    plt.gca().xaxis.set_major_locator(autodates)

    plt.savefig("./realtime_figure/" + fig_name(txtName), dpi=500)
    plt.close()
Beispiel #4
0
def make_graph_cumm(df, what_to_display):
    with _lock:
        fig1yza, ax = subplots()
        ax3 = ax.twinx()
        ax.set_title('Cummulative cases and growth COVID-19 à la Levitt')
        ax.scatter(df["date"],
                   df["what_to_display_cumm"].values,
                   color="green",
                   s=1,
                   label=f"reality {what_to_display}")
        ax.scatter(df["date"],
                   df["what_to_display_predicted_cumm"].values,
                   color="#00b3b3",
                   s=1,
                   label=f"predicted {what_to_display}")

        ax3.scatter(df["date"],
                    df["real_growth"].values,
                    color="#b300b3",
                    s=1,
                    label="real growth")
        ax3.scatter(df["date"],
                    df["predicted_growth"].values,
                    color="#b3bbb3",
                    s=1,
                    label="predicted growth")
        ax.set_xlim(df.index[0], df.index[-1])
        ax.yaxis.set_major_formatter(
            StrMethodFormatter('{x:,.0f}'))  # comma separators
        ax.grid()
        ax.legend(loc="upper left")
        ax.xaxis.set_major_formatter(
            ConciseDateFormatter(AutoDateLocator(), show_offset=False))
        st.pyplot(fig1yza)
Beispiel #5
0
def gen_temp_plot(temperature, maximums, minimums, timestamps):
    # create figure with one axes
    fig, ax = plt.subplots()

    # prettify
    ax.xaxis.set_major_formatter(ConciseDateFormatter(AutoDateLocator()))
    fig.autofmt_xdate(rotation=40)
    fig.tight_layout()

    # plot all the data at one axes
    ax.plot(timestamps, temperature, 'ko')
    ax.plot(timestamps, maximums, 'r')
    ax.plot(timestamps, minimums, 'b')

    # create a bytes stream
    img = io.BytesIO()
    # save figure to stream
    plt.savefig(img, format='png')
    img.seek(0)

    # encode bytes-like object using base64, than decode binary data
    plot_url = base64.b64encode(img.getvalue()).decode()
    # close bytes stream
    img.close()

    return plot_url
Beispiel #6
0
def _adjust_axe_timeaxis_view(ax: Axes) -> Axes:
    locator = AutoDateLocator()
    daysFmt = DateFormatter("%y%m%d\n%H:%M")
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(daysFmt)
    ax.autoscale_view()
    return ax
Beispiel #7
0
def defaultPlotOptions():
    default = {}
    default["colsPerRow"] = 2
    default["suptitle"] = "Suptitle"
    default["title"] = "Title"
    default["logx"] = False
    default["logxalt"] = False
    default["logy"] = False
    default["logyalt"] = False
    default["grid"] = True
    default["xlim"] = False
    default["xlimalt"] = False
    default["ylim"] = False
    default["ylimalt"] = False
    default["xlabel"] = ""
    default["ylabel"] = ""
    default["timeTicks"] = AutoDateLocator()
    default["timeFormat"] = default["timeTicks"]
    default["timeNum"] = 8
    default["cTitle"] = "Time"
    default["clim"] = [0.0, 1.0]
    default["rowSize"] = 7
    default["colSize"] = 8
    default["notime"] = False
    default["hist"] = False
    default["gradients"] = False
    default["eIndex"] = -1
    return default
Beispiel #8
0
def plot_percentiles(sim, percentiles, obs=None):
    discharges = [s.region_model.statistics.discharge([0]) for s in sim]
    times = utc_to_greg(np.array([discharges[0].time(i) for i in range(discharges[0].size())], dtype='d'))
    all_discharges = np.array([d.v for d in discharges])
    perc_arrs = [a for a in np.percentile(all_discharges, percentiles, 0)]
    h, fill_handles = plot_np_percentiles(times, perc_arrs, base_color=(51/256, 102/256, 193/256))
    percentile_texts = ["{} - {}".format(percentiles[i], percentiles[-(i + 1)]) for i in range(len(percentiles)//2)]
    ax = plt.gca()
    maj_loc = AutoDateLocator(tz=pytz.UTC, interval_multiples=True)
    ax.xaxis.set_major_locator(maj_loc)
    set_calendar_formatter(Calendar())
    if len(percentiles) % 2:
        fill_handles.append(h[0])
        percentile_texts.append("{}".format(percentiles[len(percentiles)//2]))
    if obs is not None:
        h_obs = plot_results(None, obs)
        fill_handles.append(h_obs)
        percentile_texts.append("Observed")

    ax.legend(fill_handles, percentile_texts)
    ax.grid(b=True, color=(51/256, 102/256, 193/256), linewidth=0.1, linestyle='-', axis='y')
    plt.xlabel("Time in UTC")
    plt.ylabel(r"Discharge in $\mathbf{m^3s^{-1}}$", verticalalignment="top", rotation="horizontal")
    ax.yaxis.set_label_coords(0, 1.1)
    return h, ax
Beispiel #9
0
    def _make_plot(self):
        try:
            from pandas.plotting._timeseries import (_decorate_axes,
                                                     format_dateaxis)
        except ImportError:
            from pandas.tseries.plotting import _decorate_axes, format_dateaxis
        plotf = self._get_plot_function()
        ax = self._get_ax(0)

        data = self.data
        data.index.name = 'Date'
        data = data.to_period(freq=self.freq)
        index = data.index
        data = data.reset_index(level=0)

        if self._is_ts_plot():
            data['Date'] = data['Date'].apply(lambda x: x.ordinal)
            _decorate_axes(ax, self.freq, self.kwds)
            candles = plotf(data, ax, **self.kwds)
            if PANDAS_0200:
                format_dateaxis(ax, self.freq, index)
            else:
                format_dateaxis(ax, self.freq)
        else:
            from matplotlib.dates import date2num, AutoDateFormatter, AutoDateLocator

            data['Date'] = data['Date'].apply(
                lambda x: date2num(x.to_timestamp()))
            candles = plotf(data, ax, **self.kwds)

            locator = AutoDateLocator()
            ax.xaxis.set_major_locator(locator)
            ax.xaxis.set_major_formatter(AutoDateFormatter(locator))

        return candles
Beispiel #10
0
    def graph_setup(self, *args):
        """
        Handle graph setup
        """

        plt.style.use('dark_background')

        loc = AutoDateLocator()
        formatter = DateFormatter('%m-%d-%y\n%H:%M')
        dates = []
        s = []
        for idx in range(0, len(self.predicter.result), 24):
            date = self.predicter.result[idx]
            dates.append(
                datetime.datetime(date[0].year, date[0].month, date[0].day,
                                  date[0].hour, date[0].minute))
            s.append(round(sum(date[2]) / date[4] * 100))
        fig, ax = plt.subplots()

        ax.xaxis.set_major_locator(loc)
        ax.xaxis.set_major_formatter(formatter)
        ax.set_facecolor('#232323')
        fig.patch.set_facecolor('#2a2a2a')
        plt.plot_date(dates,
                      s,
                      linestyle='solid',
                      marker='None',
                      color='#00f946',
                      linewidth=1)
        plt.ylabel('Efficiency', fontproperties=avenir3, fontsize=10)
        plt.xlabel('Date', fontproperties=avenir3, fontsize=10)
        plt.yticks(fontproperties=segoeui, fontsize=8)
        plt.xticks(fontproperties=segoeui, fontsize=7)
        plt.grid(color='#686868', linestyle=':', linewidth=.5)
        self.ids.test.add_widget(FigureCanvasKivyAgg(plt.gcf()))
Beispiel #11
0
def flavor_month(flavor_type):
    data_ori = pd.Series()
    for file in fileList:
        df = pd.read_csv(base_path+'\\'+file, header=None, names=['type', 'day'], sep='\t', usecols=[1, 2])
        if(flavor_type in df['type'].values):
            a = df.groupby(by='type').get_group(flavor_type)['day'].value_counts().sort_index()
            data_ori=data_ori.append(a)

    date_time = list(data_ori.index)
    value_l = []
    for ind in range(0, len(date_l)):
        if date_l[ind] in date_time:
            val_tmp=date_l[ind]
            tmp = data_ori[val_tmp]
        else:
            tmp = 0
        value_l.append(tmp)
    plt.figure()
    data_time_translation = [datetime.strptime(d, '%Y-%m-%d').date() for d in date_l]
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))  # 显示时间坐标的格式
    autodates = AutoDateLocator()  # 时间间隔自动选取
    plt.plot(data_time_translation, value_l, 'b', lw=2.5)
    plt.gcf().autofmt_xdate()  # 自动旋转日期标记
    plt.grid(True)
    plt.axis("tight")
    plt.xlabel('Time')
    plt.ylabel('count')
    plt.title('flavor')
    plt.gca().xaxis.set_major_locator(autodates)
    plt.legend(loc=0)
    # plt.show()
    plt.savefig(base_path+r'\全部'+'\\'+flavor_type + '.png')
    plt.close('all')
Beispiel #12
0
def data_visualize(train_data={}):
    x_date = []
    y_num = []

    if len(train_data) == 0:
        return -1
    #要绘制的Vm类型
    #target2plot = train_data.keys()时将在一张图绘制所有vm数据
    #可赋值['num']绘制对应的vm数据
    target2plot = train_data.keys()  #['4'] #['5','6','7','8','9']

    #绘图属性设置
    figure = pyplot.figure()
    autodates = AutoDateLocator()
    yearsFmt = DateFormatter('%Y-%m-%d')
    ax = figure.add_subplot(212)
    ax.xaxis.set_major_locator(autodates)  #设置时间间隔
    ax.xaxis.set_major_formatter(yearsFmt)  #设置时间显示格式
    ax.set_xlabel('Time')
    ax.set_ylabel('Numbers')
    ax.set_title('Data Visualizaiton')
    #    figure.axes.
    for target in target2plot:
        #        for key in train_data[target]:
        ##            x_date.append(datetime.strptime(key, "%Y-%m-%d")) # datetime类型list
        #             x_date.append(key)  #str类型list
        x_date = train_data[target].keys()
        #对时间数据进行排序处理,便于绘图
        x_date.sort()
        print date2num(x_date)
        for key in x_date:
            y_num.append((train_data[target])[key])

        print y_num
        ax.plot_date(datestr2num(x_date), y_num, '-', label="flavor" + target)
        ax.legend()
        figure.autofmt_xdate()

        #        x_num = date2num(x_date)
        #        if len(x_num) < 2:
        #            print "train data not enough"
        #        else:
        #            lq = LeastSquare(x_num, y_num, defaultOrder=4)
        #            y_pre = lq.predict(x_num)
        #
        #
        ##        print x_num, y_num, y_pre
        #        bx = figure.add_subplot(212)
        #        bx.scatter(x_num, y_num)
        #        bx.plot(x_num, y_pre)
        #
        #        x_test = arange(x_num[0],x_num[len(x_num)-1],0.1)
        #        y_test = lq.predict(x_test)
        #        bx.plot(x_test, y_test)

        #        print x_date
        #        print datestr2num(x_date)
        #清空list下个循环再见
        x_date = []
        y_num = []
 def plot_hourly(self, context, node_id, limits):
     plt.yscale('log', basey=1000, subsy=range(100, 1000, 100))
     plt.yticks([10**0, 10**3, 10**6, 10**9, 10**12, 10**15, 10**18],
                ['1 B', '1 KB', '1 MB', '1 GB', '1 TB', '1 PB', '1 EB'])
     loc = AutoDateLocator(tz=pytz.timezone('US/Eastern'))
     plt.gca().xaxis.set_major_locator(loc)
     plt.gca().xaxis.set_major_formatter(AutoDateFormatter(loc))
     plt.grid(b=True, axis='x')
     plt.grid(b=True, axis='y', which='both')
     plt.bar(*zip(*context.bytes_per_hour[node_id].items()),
             color='#50a050',
             linewidth=0,
             width=1 / 24.,
             label='total')
     if 80 in context.bytes_per_port_per_hour[node_id]:
         plt.bar(
             *zip(*context.bytes_per_port_per_hour[node_id][80].items()),
             color='#5050a0',
             linewidth=0,
             width=1 / 24.,
             label='HTTP')
     plt.xlabel('Time in EST/EDT')
     plt.ylabel('Bytes transferred per hour')
     plt.legend(prop=dict(size=10))
     plt.ylim(bottom=1)
Beispiel #14
0
def plot_linechart(df, xcol, ycol, ax=None, title="", show_peak=True):
    ax = df.plot(x=xcol,
                 y=[ycol],
                 title=title,
                 figsize=(8, 5),
                 grid=True,
                 ax=ax,
                 lw=3)
    mn_l = DayLocator()
    ax.xaxis.set_minor_locator(mn_l)
    mj_l = AutoDateLocator()
    mj_f = ConciseDateFormatter(mj_l, show_offset=False)
    ax.xaxis.set_major_formatter(mj_f)
    ax.legend(loc=2)

    if show_peak:
        # plot peak in agg
        peakx = df[ycol].idxmax()
        peak = df.loc[peakx]
        peak_desc = peak[xcol].strftime("%d-%b") + "\n" + str(int(peak[ycol]))
        _ = ax.annotate(peak_desc,
                        xy=(peak[xcol] + dt.timedelta(days=1), peak[ycol]),
                        xytext=(peak[xcol] + dt.timedelta(days=45),
                                peak[ycol] * .9),
                        arrowprops={},
                        bbox={'facecolor': 'white'})
        _ = ax.axvline(x=peak[xcol], linewidth=1, color='r')

    return ax
Beispiel #15
0
def display_inputdata(input_data, time_data=None, label=['']):
    colorstyle = ['r', 'b', 'g', 'tan', 'k', 'y', 'm']
    yearsFmt = DateFormatter('%Y-%m')
    autodates = AutoDateLocator()

    fig = plt.figure(figsize=(16, 12), dpi=160)
    ax1 = fig.add_subplot(211)
    fig.autofmt_xdate()  #设置x轴时间外观
    ax1.xaxis.set_major_locator(autodates)  #设置时间间隔
    ax1.xaxis.set_major_formatter(yearsFmt)  #设置时间显示格式

    plt.title('WaterQualite')
    plt.grid(True)
    plt.xlabel("year-month")
    time_begin = datetime.datetime(1990, 1, 1)
    time_end = datetime.datetime(2007, 12, 1)
    plt.xlim(time_begin, time_end)

    plt.ylabel("WaterQualite")

    if time_data != None:
        x = time_data
        for i in range(np.shape(input_data)[0]):
            y = np.array(input_data[i, :])[0]
            plt.plot(x, y, colorstyle[i], linewidth=2.0, label=label[i])
        plt.legend(loc="upper left", shadow=True)
    plt.show()
def plot(df, df_prev, xlabel="ds", ylabel="y"):
    from matplotlib import pyplot as plt
    from matplotlib.dates import (
        AutoDateLocator,
        AutoDateFormatter,
    )

    fig = plt.figure(facecolor="w", figsize=(10, 6))
    ax = fig.add_subplot(111)

    fcst_t = pd.to_datetime(df["ds"])
    ax.plot(pd.to_datetime(df_prev["ds"]), df_prev["y"], "k.")
    ax.plot(fcst_t, df["yhat"], ls="-", c="#0072B2")
    if "cap" in df:
        ax.plot(fcst_t, df["cap"], ls="--", c="k")

    if "floor" in df:
        ax.plot(fcst_t, df["floor"], ls="--", c="k")

    ax.fill_between(
        fcst_t, df["yhat_lower"], df["yhat_upper"], color="#0072B2", alpha=0.2
    )
    locator = AutoDateLocator(interval_multiples=False)
    formatter = AutoDateFormatter(locator)
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(formatter)
    ax.grid(True, which="major", c="gray", ls="-", lw=1, alpha=0.2)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    fig.tight_layout()

    return fig
Beispiel #17
0
def showData(stock, predict):
    data_time = [
        datetime.strptime(d, "%Y%m%d").date() for d in stock["trade_date"]
    ]

    # 配置时间坐标轴
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
    plt.gca().xaxis.set_major_locator(AutoDateLocator())  # 时间间隔自动选取

    # 绘制历史数据
    plt.plot(data_time,
             stock["low"].values,
             label="low",
             color="yellow",
             lw=1.5)
    plt.plot(data_time,
             predicted[:, 0] * stock["low"][0],
             label="predicted_low",
             color="red",
             lw=1.5)

    plt.gcf().autofmt_xdate()  # 自动旋转日期标记

    # 绘图细节
    plt.grid(True)
    plt.axis("tight")
    plt.xlabel("Time", size=20)
    plt.ylabel("Price", size=20)
    plt.title("Graph", size=20)
    plt.legend(loc=0)  # 添加图例

    plt.show()  # 显示画布
Beispiel #18
0
 def __init__(self, parent=None):
     super(SimpleViewer, self).__init__(parent)
     self.setupUi(self)
     self.figure = MplCanvas(self.centralwidget)
     self.ax = None
     self.ylim = (0, 0)
     self.lfpfactor = 1
     self.sleepstages = []
     self.ts_start_nlx = None
     self.ts_start_mpl = None
     self.use_date = False
     self.montage = None
     self.positions = None
     self.locator = AutoDateLocator()
     self.formatter = FuncFormatter(fmtfunc)
     self.offset = 150
     self.setup_gui()
     self.init_traces()
     t1 = time()
     self.init_h5man()
     dt = time() - t1
     debug('Init h5 took {:.1f} s'.format(dt))
     self.setopts()
     self.labelFolder.setText(os.path.split(os.getcwd())[1])
     self.init_montages()
     self.init_realtime()
     self.display_sleep = None
     if os.path.exists('sleepstages_clean.npy'):
         self.display_sleep = np.load('sleepstages_clean.npy')
     elif os.path.exists('sleepstages.npy'):
         self.display_sleep = np.load('sleepstages.npy')
Beispiel #19
0
def plot(m, fcst, splitFlag, ax=None, uncertainty=True, plot_cap=True, xlabel='时间', ylabel='流量',
         figsize=(10, 6)):
    """ prophet 绘图 """
    if ax is None:
        fig = plt.figure(facecolor='w', figsize=figsize)
        ax = fig.add_subplot(111)
    else:
        fig = ax.get_figure()
    fcst_t = fcst['ds'].dt.to_pydatetime()
    ax.plot(m.history['ds'].dt.to_pydatetime(), m.history['y'], 'k.')
    # ax.plot(fcst_t, fcst['yhat'], ls='-', c='#0072B2')
    ax.plot(fcst_t[0: -splitFlag], fcst['yhat'][0: -splitFlag], ls='-', c='#0072B2')
    ax.plot(fcst_t[-splitFlag:], fcst['yhat'][-splitFlag:], ls='-', c='r')
    if uncertainty:
        ax.fill_between(fcst_t, fcst['yhat_lower'], fcst['yhat_upper'],
                        color='#0072B2', alpha=0.2)

    # Specify formatting to workaround matplotlib issue #12925
    locator = AutoDateLocator(interval_multiples=False)
    formatter = AutoDateFormatter(locator)
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(formatter)
    ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    fig.tight_layout()
    return fig
Beispiel #20
0
def plot_forecast_component(m, fcst, name, ax=None, uncertainty=True, plot_cap=False,
                            figsize=(10, 6), ylabel=""):
    artists = []
    if not ax:
        fig = plt.figure(facecolor='w', figsize=figsize)
        ax = fig.add_subplot(111)
    fcst_t = fcst['ds'].dt.to_pydatetime()
    artists += ax.plot(fcst_t, fcst[name], ls='-', c='#0072B2')
    if 'cap' in fcst and plot_cap:
        artists += ax.plot(fcst_t, fcst['cap'], ls='--', c='k')
    if m.logistic_floor and 'floor' in fcst and plot_cap:
        ax.plot(fcst_t, fcst['floor'], ls='--', c='k')
    if uncertainty:
        artists += [ax.fill_between(
            fcst_t, fcst[name + '_lower'], fcst[name + '_upper'],
            color='#0072B2', alpha=0.2)]
    # Specify formatting to workaround matplotlib issue #12925
    locator = AutoDateLocator(interval_multiples=False)
    formatter = AutoDateFormatter(locator)
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(formatter)
    ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
    ax.set_xlabel('时间')

    ax.set_ylabel(name if ylabel == "" else ylabel)
    if name in m.component_modes['multiplicative']:
        ax = set_y_as_percent(ax)
    return artists
Beispiel #21
0
def plot(
        m,
        fcst,
        ax=None,
        uncertainty=True,
        plot_cap=True,
        xlabel='ds',
        ylabel='y',
        figsize=(10, 6),
        y_column='y',
):
    """Plot the Prophet forecast.

    Parameters
    ----------
    m: Prophet model.
    fcst: pd.DataFrame output of m.predict.
    ax: Optional matplotlib axes on which to plot.
    uncertainty: Optional boolean to plot uncertainty intervals, which will
        only be done if m.uncertainty_samples > 0.
    plot_cap: Optional boolean indicating if the capacity should be shown
        in the figure, if available.
    xlabel: Optional label name on X-axis
    ylabel: Optional label name on Y-axis
    figsize: Optional tuple width, height in inches.

    Returns
    -------
    A matplotlib figure.
    """
    if ax is None:
        fig = plt.figure(facecolor='w', figsize=figsize)
        ax = fig.add_subplot(111)
    else:
        fig = ax.get_figure()
    fcst_t = fcst['ds'].dt.to_pydatetime()
    ax.plot(m.history['ds'].dt.to_pydatetime(), m.history[y_column], 'k.')
    ax.plot(fcst_t, fcst['yhat'], ls='-', c='#0072B2')
    if 'cap' in fcst and plot_cap:
        ax.plot(fcst_t, fcst['cap'], ls='--', c='k')
    if m.logistic_floor and 'floor' in fcst and plot_cap:
        ax.plot(fcst_t, fcst['floor'], ls='--', c='k')
    if uncertainty and m.uncertainty_samples:
        ax.fill_between(fcst_t,
                        fcst['yhat_lower'],
                        fcst['yhat_upper'],
                        color='#0072B2',
                        alpha=0.2)
    # Specify formatting to workaround matplotlib issue #12925
    locator = AutoDateLocator(interval_multiples=False)
    formatter = AutoDateFormatter(locator)
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(formatter)
    ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    fig.tight_layout()
    return fig
Beispiel #22
0
    def plot_column(self,
                    column,
                    region='',
                    state='',
                    city='',
                    ax=None,
                    data_source='Ministério da Saúde',
                    add_moving_average=False,
                    start_date=None,
                    end_date=None):
        '''(String, String, String, String) -> None
        Plota o gráfico de uma métrica em um local selecionado'''

        if self.valid_col(column, data_source):

            df = self.get_df(region, state, city, data_source, start_date,
                             end_date)

            if not df.empty:

                if ax is None:
                    fig = plt.figure(facecolor='w', figsize=(10, 6))
                    ax = fig.add_subplot(111)
                    show = True
                else:
                    fig = ax.get_figure()
                    show = False

                locator = AutoDateLocator(interval_multiples=False)
                formatter = AutoDateFormatter(locator)
                ax.xaxis.set_major_locator(locator)
                ax.xaxis.set_major_formatter(formatter)
                ax.plot(df['date'],
                        df[column],
                        label='%s | %s' %
                        (self.correct_col_name[data_source][column],
                         self.format_location([region, state, city])))

                if add_moving_average:
                    ax.plot(df['date'], df[column].expanding(min_periods=60).mean(), \
                            label='Média Móvel | %s | %s' % (self.correct_col_name[data_source][column], self.format_location( [region, state, city] ) ) )

                if show:
                    fig.legend()
                    plt.show()
            else:
                self.messagebox.show_message(self.not_found_msg(
                    region, state, city),
                                             type='Erro')

        else:
            valid_vals = [
                k for k in self.correct_col_name[data_source] if k != 'date'
            ]
            self.messagebox.show_message(
                '''\nO nome %s não corresponde a uma coluna válida para dados do %s. 
            Os nomes válidos para colunas do conjunto atual são:\n- %s \n''' %
                (data_source, column, '\n- '.join(valid_vals)))
Beispiel #23
0
def plot_states_and_var(data,
                        hidden_states,
                        cmap=None,
                        columns=None,
                        by='Activity'):
    """
    Make  a plot of the data and the states

    Parameters
    ----------
    data : pandas DataFrame
        Data to plot
    hidden_states: iteretable
        the hidden states corresponding to the timesteps
    columns : list, optional
        Which columns to plot
    by : str
        The column to group on
    """
    fig, ax = plt.subplots(figsize=(15, 5))
    if columns is None:
        columns = data.columns
    df = data[columns].copy()
    stateseq = np.array(hidden_states)
    stateseq_norep, durations = rle(stateseq)
    datamin, datamax = np.array(df).min(), np.array(df).max()
    y = np.array([datamin, datamax])
    maxstate = stateseq.max() + 1
    x = np.hstack(([0], durations.cumsum()[:-1], [len(df.index) - 1]))
    C = np.array([[float(state) / maxstate]
                  for state in stateseq_norep]).transpose()
    ax.set_xlim((min(x), max(x)))

    if cmap is None:
        num_states = max(hidden_states) + 1
        colormap, cmap = get_color_map(num_states)
    pc = ax.pcolorfast(x, y, C, vmin=0, vmax=1, alpha=0.3, cmap=cmap)
    plt.plot(df.as_matrix())
    locator = AutoDateLocator()
    locator.create_dummy_axis()
    num_index = pd.Index(df.index.map(date2num))
    ticks_num = locator.tick_values(min(df.index), max(df.index))
    ticks = [num_index.get_loc(t) for t in ticks_num]
    plt.xticks(ticks, df.index.strftime('%H:%M')[ticks], rotation='vertical')
    cb = plt.colorbar(pc)
    cb.set_ticks(np.arange(1. / (2 * cmap.N), 1, 1. / cmap.N))
    cb.set_ticklabels(np.arange(0, cmap.N))
    # Plot the activities
    if by is not None:
        actseq = np.array(data[by])
        sca = ax.scatter(
            np.arange(len(hidden_states)),  #data.index,
            np.ones_like(hidden_states) * datamax,
            c=actseq,
            edgecolors='none')
    plt.show()
    return fig, ax
Beispiel #24
0
def plot_trend(m,
               ax=None,
               plot_name="Trend",
               figsize=(10, 6),
               df_name="__df__"):
    """Make a barplot of the magnitudes of trend-changes.

    Args:
        m (NeuralProphet): fitted model.
        ax (matplotlib axis): matplotlib Axes to plot on.
            One will be created if this is not provided.
        plot_name (str): Name of the plot Title.
        figsize (tuple): width, height in inches. Ignored if ax is not None.
             default: (10, 6)
        df_name: name of dataframe to refer to data params from original list of train dataframes (used for local normalization in global modeling)

    Returns:
        a list of matplotlib artists
    """
    artists = []
    if not ax:
        fig = plt.figure(facecolor="w", figsize=figsize)
        ax = fig.add_subplot(111)
    data_params = m.config_normalization.get_data_params(df_name)
    t_start = data_params["ds"].shift
    t_end = t_start + data_params["ds"].scale
    if m.config_trend.n_changepoints == 0:
        fcst_t = pd.Series([t_start, t_end]).dt.to_pydatetime()
        trend_0 = m.model.bias.detach().numpy()
        if m.config_trend.growth == "off":
            trend_1 = trend_0
        else:
            trend_1 = trend_0 + m.model.trend_k0.detach().numpy()

        data_params = m.config_normalization.get_data_params(df_name)
        shift = data_params["y"].shift
        scale = data_params["y"].scale
        trend_0 = trend_0 * scale + shift
        trend_1 = trend_1 * scale + shift
        artists += ax.plot(fcst_t, [trend_0, trend_1], ls="-", c="#0072B2")
    else:
        days = pd.date_range(start=t_start, end=t_end, freq=m.data_freq)
        df_y = pd.DataFrame({"ds": days})
        df_trend = m.predict_trend(df={df_name: df_y})[df_name]
        artists += ax.plot(df_y["ds"].dt.to_pydatetime(),
                           df_trend["trend"],
                           ls="-",
                           c="#0072B2")
    # Specify formatting to workaround matplotlib issue #12925
    locator = AutoDateLocator(interval_multiples=False)
    formatter = AutoDateFormatter(locator)
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(formatter)
    ax.grid(True, which="major", c="gray", ls="-", lw=1, alpha=0.2)
    ax.set_xlabel("ds")
    ax.set_ylabel(plot_name)
    return artists
Beispiel #25
0
def manip_info(sessionName, quiet, line_to_print, var_to_plot):
    """
    This function prints information about a session, and optionally
    plot specified variables.

    It can be accessed from the CLI tool ManipInfo
    """

    if os.path.exists(sessionName + ".db"):
        SavedAsyncSession(sessionName).print_description()
        return

    if sessionName.endswith(".hdf5"):
        N = len(sessionName)
        sessionName = sessionName[0:(N - 5)]

    MI = Manip(sessionName).MI
    if line_to_print is not None:
        if line_to_print >= len(MI.log("t")):
            print("Specified line is out of bound.")
            sys.exit(1)
        format_str = "{:>15} | {:>20}"
        print("Printing saved values on line", line_to_print)
        print(format_str.format("Variable", "Value"))
        varlist = ["Time"]
        varlist += MI.log_variable_list()
        print("-" * 38)
        for varname in varlist:
            valtab = MI.log(varname)
            if isinstance(valtab, (float, int)):
                # might occur if only one line
                print(format_str.format(varname, MI.log(varname)))
            else:
                print(
                    format_str.format(varname,
                                      MI.log(varname)[line_to_print]))
    elif not quiet:
        MI.describe()
    if var_to_plot is not None:
        if var_to_plot in MI.log_variable_list():
            t = epoch2num(MI.log("t"))
            vardata = MI.log(var_to_plot)
            fig = plt.figure()
            xtick_locator = AutoDateLocator()
            xtick_formatter = AutoDateFormatter(xtick_locator)
            ax = plt.axes()
            ax.xaxis.set_major_locator(xtick_locator)
            ax.xaxis.set_major_formatter(xtick_formatter)
            ax.plot(t, vardata, "o-")
            plt.setp(ax.xaxis.get_majorticklabels(), rotation=70)
            fig.subplots_adjust(bottom=0.2)
            plt.ylabel(var_to_plot)
            plt.title(sessionName)
            plt.show()
        else:
            print("Variable", var_to_plot, "does not exist!")
            sys.exit(1)
Beispiel #26
0
def showBtResult(d):
    """
    显示回测结果
    """
    name = d.get('name')
    timeList = d['timeList']
    pnlList = d['pnlList']
    capitalList = d['capitalList']
    drawdownList = d['drawdownList']

    print(u'显示回测结果')
    # 输出
    if len(timeList) > 0:
        print('-' * 30)
        print(u'第一笔交易:\t%s' % d['timeList'][0])
        print(u'最后一笔交易:\t%s' % d['timeList'][-1])

        print(u'总交易次数:\t%s' % formatNumber(d['totalResult']))
        print(u'总盈亏:\t%s' % formatNumber(d['capital']))
        print(u'最大回撤: \t%s' % formatNumber(min(d['drawdownList'])))

        print(u'平均每笔盈亏:\t%s' % formatNumber(d['capital'] / d['totalResult']))
        print(u'平均每笔佣金:\t%s' %
              formatNumber(d['totalCommission'] / d['totalResult']))

        print(u'胜率\t\t%s%%' % formatNumber(d['winningRate']))
        print(u'平均每笔盈利\t%s' % formatNumber(d['averageWinning']))
        print(u'平均每笔亏损\t%s' % formatNumber(d['averageLosing']))
        print(u'盈亏比:\t%s' % formatNumber(d['profitLossRatio']))
        print(u'显示回测结果')

        # 绘图
        import matplotlib.pyplot as plt
        from matplotlib.dates import AutoDateLocator, DateFormatter
        autodates = AutoDateLocator()
        yearsFmt = DateFormatter('%m-%d')

        pCapital = plt.subplot(3, 1, 1)
        pCapital.set_ylabel("capital")
        pCapital.plot(timeList, capitalList)
        plt.gcf().autofmt_xdate()  #设置x轴时间外观
        plt.gcf().subplots_adjust(bottom=0.1)
        plt.gca().xaxis.set_major_locator(autodates)  #设置时间间隔
        plt.gca().xaxis.set_major_formatter(yearsFmt)  #设置时间显示格式

        pDD = plt.subplot(3, 1, 2)
        pDD.set_ylabel("dd")
        pDD.bar(range(len(drawdownList)), drawdownList)

        pPnl = plt.subplot(3, 1, 3)
        pPnl.set_ylabel("pnl")
        pPnl.hist(pnlList, bins=20)

        plt.subplots_adjust(bottom=0.05, hspace=0.3)
        plt.show()
Beispiel #27
0
    def draw_2(self, id):

        data = libchart.get_chart_data(self.session,
                                       self.dt_start,
                                       self.dt_end,
                                       [[id, datetime.timedelta()]],
                                       daily=True)

        x = []
        y = []
        for k, v in data.iteritems():
            x.append(datetime.datetime.strptime(k, '%Y%m%d0000'))
            if v[0] is None:
                y.append(1)
            else:
                y.append(v[0])

        fig = plt.figure(figsize=(400 / 80, 200 / 80))
        ax = fig.add_subplot(1, 1, 1)
        ax.bar(x, y, width=0.4, align='center', color='blue')
        ax.grid(True, axis='y')

        ax.set_ylim(0)
        locator = AutoDateLocator()
        formatter = AutoDateFormatter(locator)
        formatter.scaled[(1.)] = '%m-%d'
        ax.xaxis.set_major_locator(locator)
        ax.xaxis.set_major_formatter(formatter)

        def yaxis_formatter(y, pos):
            def r(i):
                i = round(i, 2)
                if i == int(i):
                    i = int(i)
                return i

            if y < 1e3:
                return int(y)
            elif y < 1e6:
                return '%sK' % r(y / 1e3)
            else:
                return '%sM' % r(y / 1e6)

        ax.yaxis.set_major_formatter(FuncFormatter(yaxis_formatter))

        for tick in ax.xaxis.get_major_ticks():
            tick.label1.set_size(9)
        for tick in ax.yaxis.get_major_ticks():
            tick.label1.set_size(9)

        f = StringIO.StringIO()
        fig.savefig(f)
        plt.close()
        f.seek(0)
        return f
 def plot_daily_linear(self, context, node_id, limits):
     loc = AutoDateLocator(tz=pytz.timezone('US/Eastern'))
     plt.gca().xaxis.set_major_locator(loc)
     plt.gca().xaxis.set_major_formatter(AutoDateFormatter(loc))
     plt.bar(*zip(*context.bytes_per_day[node_id].items()),
             color='#50a050',
             linewidth=0,
             width=1)
     plt.xlabel('Time in EST/EDT')
     plt.ylabel('Bytes transferred per day')
     plt.ylim(bottom=1)
Beispiel #29
0
def plot_forecast_component(m,
                            fcst,
                            name,
                            ax=None,
                            uncertainty=True,
                            plot_cap=False,
                            figsize=(10, 6)):
    """Plot a particular component of the forecast.

    Parameters
    ----------
    m: Prophet model.
    fcst: pd.DataFrame output of m.predict.
    name: Name of the component to plot.
    ax: Optional matplotlib Axes to plot on.
    uncertainty: Optional boolean to plot uncertainty intervals, which will
        only be done if m.uncertainty_samples > 0.
    plot_cap: Optional boolean indicating if the capacity should be shown
        in the figure, if available.
    figsize: Optional tuple width, height in inches.

    Returns
    -------
    a list of matplotlib artists
    """
    artists = []
    if not ax:
        fig = plt.figure(facecolor='w', figsize=figsize)
        ax = fig.add_subplot(111)
    fcst_t = fcst['ds'].dt.to_pydatetime()
    artists += ax.plot(fcst_t, fcst[name], ls='-', c='#0072B2')
    if 'cap' in fcst and plot_cap:
        artists += ax.plot(fcst_t, fcst['cap'], ls='--', c='k')
    if m.logistic_floor and 'floor' in fcst and plot_cap:
        ax.plot(fcst_t, fcst['floor'], ls='--', c='k')
    if uncertainty and m.uncertainty_samples:
        artists += [
            ax.fill_between(fcst_t,
                            fcst[name + '_lower'],
                            fcst[name + '_upper'],
                            color='#0072B2',
                            alpha=0.2)
        ]
    # Specify formatting to workaround matplotlib issue #12925
    locator = AutoDateLocator(interval_multiples=False)
    formatter = AutoDateFormatter(locator)
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(formatter)
    ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
    ax.set_xlabel('ds')
    ax.set_ylabel(name)
    if name in m.component_modes['multiplicative']:
        ax = set_y_as_percent(ax)
    return artists
Beispiel #30
0
def print_mat():
    fig1 = plt.figure()
    ax1 = fig1.add_subplot(1, 1, 1)
    ax1.xaxis.set_major_formatter(DateFormatter('%m-%d'))  #设置时间标签显示格式
    ax1.xaxis.set_major_locator(AutoDateLocator())  #设置x轴自动时间刻度
    ax1.yaxis.set_major_locator(AutoLocator())  #设置y轴自动刻度
    plt.plot(Date, Open, label="Open", color="red")
    plt.plot(Date, Close, label="Close", color="blue")
    plt.title(u"抓取特斯拉股票历史开盘、收盘价格", fontproperties=font)
    plt.xlabel("时间", fontproperties=font)
    plt.legend()
    plt.show()