<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def plot_timestream(array, kidid, xtick='time', scantypes=None, ax=None, **kwargs):
 """Plot timestream data. Args: array (xarray.DataArray):
 Array which the timestream data are included. kidid (int):
 Kidid. xtick (str):
 Type of x axis. 'time': Time. 'index': Time index. scantypes (list):
 Scantypes. If None, all scantypes are used. ax (matplotlib.axes):
 Axis you want to plot on. kwargs (optional):
 Plot options passed to ax.plot(). """

    if ax is None:
        ax = plt.gca()

    index = np.where(array.kidid == kidid)[0]
    if len(index) == 0:
        raise KeyError('Such a kidid does not exist.')
    index = int(index)

    if scantypes is None:
        if xtick == 'time':
            ax.plot(array.time, array[:, index], label='ALL', **kwargs)
        elif xtick == 'index':
            ax.plot(np.ogrid[:len(array.time)], array[:, index], label='ALL', **kwargs)
    else:
        for scantype in scantypes:
            if xtick == 'time':
                ax.plot(array.time[array.scantype == scantype],
                        array[:, index][array.scantype == scantype], label=scantype, **kwargs)
            elif xtick == 'index':
                ax.plot(np.ogrid[:len(array.time[array.scantype == scantype])],
                        array[:, index][array.scantype == scantype], label=scantype, **kwargs)
    ax.set_xlabel('{}'.format(xtick))
    ax.set_ylabel(str(array.datatype.values))
    ax.legend()

    kidtpdict = {0: 'wideband', 1: 'filter', 2: 'blind'}
    try:
        kidtp = kidtpdict[int(array.kidtp[index])]
    except KeyError:
        kidtp = 'filter'
    ax.set_title('ch #{} ({})'.format(kidid, kidtp))

    logger.info('timestream data (ch={}) has been plotted.'.format(kidid))