Python绘图更改背景颜色和线条颜色

时间:2020-02-26 05:19:53

标签: python matplotlib plot colors

link的教程介绍了如何按纪元绘制测试和训练数据的不同指标。请检查plot_metrics(baseline_history)

的输出

我只有两个指标要打印 我复制了绘图部分,并需要以下帮助

  1. 如何将测试线的颜色更改为红色?我尝试了plt.plot(history.epoch, history.history['val_'+metric], color=colors['r'], linestyle="--", label='Val'),但收到了错误TypeError: list indices must be integers or slices, not str

  2. 如何确保纯色背景。由于某种原因,我越来越 白色线条的灰色背景

我的代码和下面的输出

#plotting
def plot_metrics(history):
  metrics =  ['loss','acc']
  for n, metric in enumerate(metrics):
    name = metric.replace("_"," ").capitalize()
    plt.subplot(2,2,n+1)
    plt.plot(history.epoch,  history.history[metric], color=colors[0], label='Train')
    plt.plot(history.epoch, history.history['val_'+metric],
             color=colors[9], linestyle="--", label='Val')
    plt.xlabel('Epoch')
    plt.ylabel(name)
    if metric == 'loss':
      plt.ylim([0, plt.ylim()[1]])
    elif metric == 'auc':
      plt.ylim([0.8,1])
    else:
      plt.ylim([0,1])

    plt.legend()

import matplotlib.pyplot as plt
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']

import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (20, 12)
plot_metrics(final_model)#where abcde is fit call

enter image description here

1 个答案:

答案 0 :(得分:1)

  1. 您可以简单地尝试添加color参数。 用法:plt.plot([1,2,3,4,5,6],color ='red')

  2. 您可以使用grid(False)删除网格。 用法:plt.grid(False)