【发布时间】:2018-11-10 16:15:45
【问题描述】:
我有以下格式的数据:
| | Measurement 1 | | Measurement 2 | |
|------|---------------|------|---------------|------|
| | Mean | Std | Mean | Std |
| Time | | | | |
| 0 | 17 | 1.10 | 21 | 1.33 |
| 1 | 16 | 1.08 | 21 | 1.34 |
| 2 | 14 | 0.87 | 21 | 1.35 |
| 3 | 11 | 0.86 | 21 | 1.33 |
我正在使用以下代码从这些数据中生成一个 matplotlib 折线图,它将标准偏差显示为填充区域,如下所示:
def seconds_to_minutes(x, pos):
minutes = f'{round(x/60, 0)}'
return minutes
fig, ax = plt.subplots()
mean_temperature_over_time['Measurement 1']['mean'].plot(kind='line', yerr=mean_temperature_over_time['Measurement 1']['std'], alpha=0.15, ax=ax)
mean_temperature_over_time['Measurement 2']['mean'].plot(kind='line', yerr=mean_temperature_over_time['Measurement 2']['std'], alpha=0.15, ax=ax)
ax.set(title="A Line Graph with Shaded Error Regions", xlabel="x", ylabel="y")
formatter = FuncFormatter(seconds_to_minutes)
ax.xaxis.set_major_formatter(formatter)
ax.grid()
ax.legend(['Mean 1', 'Mean 2'])
输出:
这似乎是一个非常混乱的解决方案,并且实际上只产生阴影输出,因为我有这么多数据。从带有阴影错误区域的数据框中生成折线图的正确方法是什么?我看过Plot yerr/xerr as shaded region rather than error bars,但无法根据我的情况调整它。
【问题讨论】:
标签: python-3.x pandas matplotlib jupyter-notebook