def plot_delta_since(data_adaptor: DataAdaptor, type_: ValueType, countries: Iterable[str], ax: Axes, rolling: int = 1, start: Optional[int] = None, legend: bool = False) -> None: data_country = data_adaptor.get_time_delta_for_country( type_, countries, start, rolling) data_country[data_country <= 0] = np.NAN if start: data_country = data_country[data_country.index.days >= 0] time_data = data_country.index.days if start else data_country.index ax.plot(time_data, data_country[countries]) if legend: ax.legend(countries) ax.set_yscale("log") ax.set_ylim(data_country.min().min(), data_country.max().max()) ylabel = f"{type_.value} per day" if rolling > 1: ylabel += f" ({rolling} day moving average)" ax.set_ylabel(ylabel) if start: ax.set_xlabel(f"Days since {start} {type_.value}") else: ax.set_xlabel("Date")
def plot_data_since(data_adaptor: DataAdaptor, type_: ValueType, countries: Iterable[str], ax: Axes, start: Optional[int] = None, legend: bool = False) -> None: data_country = data_adaptor.get_time_series_for_country( type_, countries, start) time_data = data_country.index.days if start else data_country.index ax.plot(time_data, data_country[countries]) if legend: ax.legend(countries) ax.set_yscale("log") if start: ax.set_xlim(-1.0, data_country.index.days.max()) ax.set_ylim(start, data_country.max().max()) ax.set_ylabel(type_.value) if start: ax.set_xlabel(f"Days since {start} {type_.value}") else: ax.set_xlabel("Date")
def plot_history(history: dict, lbl: str, ax1: Axes, ax2: Axes, color: str) -> Tuple[Axes, Axes]: """ 根據給定的history畫圖,左為loss vs epoch,右為accuracy vs epoch Args: history (dict): keras中的history dict lbl (str): 該history對應的legend ax1 (Axes) 物件,畫線圖 (loss vs epoch) ax2 (Axes) 物件,畫線圖 (accuracy vs epoch) color (str) : 該history對應的顏色 Returns: Tuple[Axes, Axes] : 畫好線的Axes物件 """ acc_keyword = ['acc', 'accuracy'] val_acc_keyword = ['val_acc', 'val_accuracy'] ax1.plot(history['loss'], label=f'{lbl}_train', c=color) ax1.plot(history['val_loss'], label=f'{lbl}_val', c=color, linestyle='--') ax1.set_ylabel('Loss') ax1.set_xlabel('Epoch') ######### [START] for differenct keras version acc keywords ############# try: # 'acc' for keras 2.1.6 ax2.plot(history[acc_keyword[0]], label=f'{lbl}_train', c=color) ax2.plot(history[val_acc_keyword[0]], label=f'{lbl}_val', c=color, linestyle='--') except KeyError: # 'accuracy' for keras > 2.1.6 ax2.plot(history[acc_keyword[1]], label=f'{lbl}_train', c=color) ax2.plot(history[val_acc_keyword[1]], label=f'{lbl}_val', c=color, linestyle='--') ######### [END] for differenct keras version acc keywords ############# ax2.set_ylabel('accuracy') ax2.set_xlabel('Epoch') return ax1, ax2