You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除Matplotlib图表中曲线下方的填充区域

问题描述

我有一段Matplotlib绘图代码如下:

import pandas as pd
from matplotlib import pyplot as plt

columns = ['Price']
price_values = [[4.2],
    [4.1],
    [4],
    [3.8],
    [3.9],
    [4.2],
    [4.5],
    [4.8],
    [5.2],
    [5.2],
    [5.2],
    [5.6],
    [5.2],
    [5.1],
    [5.3],
    [6],
    [6.2],
    [6.3],
    [6.2],
    [6],
    [5.5] ,
    [5.2],
    [4.8],
    [4.6]]

price_data = pd.DataFrame(price_values, index=range(0, 24), columns=columns)
fig = plt.figure(linewidth=1, figsize=(9, 5))
ax=plt.gca()

for column,color in zip(price_data.columns,['gold']):
    ax.fill_between(
        x=price_data.index,
        y1=price_data[column],
        y2=0,
        label=column,
        color=color,
        alpha=.5,
        step='post',
        linewidth=5,
    )

ax.set_facecolor("white")
ax.set_xlabel("Time of day", fontsize = 14, labelpad=8)
ax.set_ylabel("Price [Cent/kWh]", fontsize = 14,labelpad=8)
ax.set_xlim(0, 23)
ax.set_ylim(0, 8)

plt.xticks(price_data.index, labels=[f'{h:02d}:00' for h in price_data.index], rotation=90)

plt.tight_layout()
hours = list(range(25))
labels = [f'{h:02d}:00' for h in hours]
ax.tick_params(axis='both', which='major', labelsize=14)
ax.legend(loc='center left', bbox_to_anchor=(0.15, 1.07), fontsize = 14, ncol=3)
plt.savefig('Diagramm.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()

我希望移除曲线下方的填充区域,仅保留曲线。尝试使用plt.bar(fill=False)时出现错误:TypeError: bar() missing 2 required positional arguments: 'x' and 'height',请问有什么解决方法?

解决方法

你之前用plt.bar()的思路不对,bar()是绘制柱状图的方法,必须传入x和height两个必填参数,而你需要的是折线图,应该用ax.plot()替代原来的ax.fill_between(),同时保留step='post'参数来保持和原填充边界一致的折线样式。

核心修改部分

将原来的fill_between代码块替换为:

for column,color in zip(price_data.columns,['gold']):
    ax.plot(
        price_data.index,
        price_data[column],
        label=column,
        color=color,
        linewidth=5,
        step='post'
    )

完整修改后的代码

import pandas as pd
from matplotlib import pyplot as plt

columns = ['Price']
price_values = [[4.2],
    [4.1],
    [4],
    [3.8],
    [3.9],
    [4.2],
    [4.5],
    [4.8],
    [5.2],
    [5.2],
    [5.2],
    [5.6],
    [5.2],
    [5.1],
    [5.3],
    [6],
    [6.2],
    [6.3],
    [6.2],
    [6],
    [5.5] ,
    [5.2],
    [4.8],
    [4.6]]

price_data = pd.DataFrame(price_values, index=range(0, 24), columns=columns)
fig = plt.figure(linewidth=1, figsize=(9, 5))
ax=plt.gca()

# 替换fill_between为plot,仅绘制折线
for column,color in zip(price_data.columns,['gold']):
    ax.plot(
        price_data.index,
        price_data[column],
        label=column,
        color=color,
        linewidth=5,
        step='post'
    )

ax.set_facecolor("white")
ax.set_xlabel("Time of day", fontsize = 14, labelpad=8)
ax.set_ylabel("Price [Cent/kWh]", fontsize = 14,labelpad=8)
ax.set_xlim(0, 23)
ax.set_ylim(0, 8)

plt.xticks(price_data.index, labels=[f'{h:02d}:00' for h in price_data.index], rotation=90)

plt.tight_layout()
hours = list(range(25))
labels = [f'{h:02d}:00' for h in hours]
ax.tick_params(axis='both', which='major', labelsize=14)
ax.legend(loc='center left', bbox_to_anchor=(0.15, 1.07), fontsize = 14, ncol=3)
plt.savefig('Diagramm.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()

关键说明

  • 移除了fill_between的填充相关参数(如y2=0、alpha=.5),改用plot绘制纯折线
  • 保留step='post'确保折线的分段样式和原填充图形的边界完全一致
  • 原代码中的linewidth=5参数直接保留,用于控制折线的粗细

内容的提问来源于stack exchange,提问作者PeterBe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 12:15:38