【问题标题】:Filling range of graph in matplotlibmatplotlib中图形的填充范围
【发布时间】:2017-07-17 05:18:36
【问题描述】:

我有一个使用matplotlib 制作的折线图。我正在尝试在线条后面添加填充,使填充区域从current_val - standard_deviationcurrent_val + standard deviation。到目前为止,我已经阅读了文档,我最初只在第一个坐标的 y 截距处得到一条填充线。

我的代码目前如下:

data_field = 'Mean'
row_index = 'Rvolt'
x_axis = buckets
y_axis = df_result.loc[df_result.index.isin([row_index]) & df_result['Bucket'].between(1, 144), data_field].tolist()
plt.plot(x_axis, y_axis)
plt.xlabel("Bucket Spans")
plt.ylabel(data_field + " values for " + row_index)
#plt.fill(x_axis, y_axis)
plt.show()

如何让我的图表最终看起来有点像

为了进一步披露,我的 x 和 y 轴是列表。

【问题讨论】:

    标签: python matplotlib anaconda


    【解决方案1】:

    假设您的 y_axis 值为 numpy 数组。否则,您必须使用 for 循环手动添加/减去 y_axis 变量的标准偏差。

    plt.fill_between(x, y_axis - standard_deviation, y_axis + standard_deviation)
    

    【讨论】:

    • 在我的情况下 ax 是 plt 还是另一个引用轴的变量?
    • 此外,当我尝试您的解决方案时,控制台给我一个错误,因为我无法在列表和 int/float 之间执行y_axis - standev 操作
    • 那是因为你的 y_axis 是一个列表而不是一个 numpy 数组。您必须使用y_axis = np.array(y_axis) 将其转换为numpy 数组,或者使用[v - std for v in y_axis] 之类的for 循环。注意:在您的解决方案中,您最终使用 np.array,但您不必使用 stddev,因为 numpy 对象将为您处理转换
    【解决方案2】:

    我使用@GWW 的答案来实现fill_between,但我传递了一个上下限列表!

    这是我的解决方案:

    data_field = 'Mean'
    row_index = 'Rvolt'
    x_axis = buckets
    y_axis = df_result.loc[df_result.index.isin([row_index]) & df_result['Bucket'].between(1, 144), data_field].tolist()
    standevs = df_result.loc[df_result.index.isin([row_index]) & df_result['Bucket'].between(1, 144), 'StanDev'].tolist()
    lower_bound = np.array(y_axis) - np.array(standevs)
    upper_bound = np.array(y_axis) + np.array(standevs)
    plt.plot(x_axis, y_axis)
    plt.xlabel("Bucket Spans")
    plt.ylabel(data_field + " values for " + row_index)
    plt.fill_between(x_axis, lower_bound, upper_bound, facecolor='lightblue')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2012-08-07
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多