使用 Matplotlib 在 Python 中填充曲线和 x 轴之间的区域 [英] Filling region between curve and x-axis in Python using Matplotlib

查看:99
本文介绍了使用 Matplotlib 在 Python 中填充曲线和 x 轴之间的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MatPlotLib在Python中简单地填充绘图曲线下方的区域.

I am trying to simply fill the area under the curve of a plot in Python using MatPlotLib.

这是我的 SSCCE:

Here is my SSCCE:

import json
import pprint
import numpy as np
import matplotlib.pyplot as plt

y = [0,0,0,0,0,0,0,0,0,0,0,863,969,978,957,764,767,1009,1895,980,791]
x = np.arange(len(y))


fig2, ax2 = plt.subplots()
ax2.fill(x, y)

plt.savefig('picForWeb.png')
plt.show()

所附图片显示了产生的输出.

The attached picture shows the output produced.

有人知道为什么Python无法填充x轴和曲线之间的整个区域吗?

Does anyone know why Python is not filling the entire area in between the x-axis and the curve?

我已经完成了Google和StackOverflow搜索,但是找不到类似的示例.直觉上它似乎应该填满曲线下的整个区域.

I've done Google and StackOverflow searches, but could not find a similar example. Intuitively it seems that it should fill the entire area under the curve.

推荐答案

对于这些类型的图,我通常使用 fill_between 函数.试试这样的:

I usually use the fill_between function for these kinds of plots. Try something like this instead:

import numpy as np
import matplotlib.pyplot as plt

y = [0,0,0,0,0,0,0,0,0,0,0,863,969,978,957,764,767,1009,1895,980,791]
x = np.arange(len(y))

fig, (ax1) = plt.subplots(1,1); 
ax1.fill_between(x, 0, y)
plt.show()

此处中查看更多示例.

这篇关于使用 Matplotlib 在 Python 中填充曲线和 x 轴之间的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆