【问题标题】:python plotting change background color and line colorpython绘图更改背景颜色和线条颜色
【发布时间】:2020-06-09 22:23:08
【问题描述】:

这个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

【问题讨论】:

    标签: python matplotlib plot colors


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

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

    【讨论】:

      猜你喜欢
      • 2018-07-25
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 2016-09-10
      相关资源
      最近更新 更多