plt.fill_between用法
时间: 2023-10-12 13:05:37 浏览: 42
plt.fill_between是matplotlib库中的一个函数,用于在两个指定的曲线之间填充颜色。具体用法是:
plt.fill_between(x, y1, y2=0, where=None, interpolate=False, step=None, *, data=None, **kwargs)
其中,x、y1、y2分别是指定曲线的横纵坐标,where是一个逻辑数组,指定哪些区域需要填充颜色,interpolate和step是用于插值和控制线段样式的参数。其他参数可以用于指定填充颜色等样式。
例如,如果要在x轴范围为[0, 1],y轴范围为[-1, 1]的区域内,填充y=x和y=-x之间的颜色,可以使用以下代码:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 1, 100)
y1 = x
y2 = -x
plt.plot(x, y1, color='b', label='y=x')
plt.plot(x, y2, color='r', label='y=-x')
plt.fill_between(x, y1, y2, where=y1>y2, color='g', alpha=0.5)
plt.legend()
plt.show()
相关问题
fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') ax.set_title('Prediction Intervals') ax.set_xlabel('Time') ax.set_ylabel('RV_5min') plt.legend() plt.show() fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') 怎么将两个ax.fill_between画在一幅图上,不相互遮挡,
你可以使用相同的 `ax` 对象来绘制多个 `fill_between` 图形,以确保它们在同一幅图上不会相互遮挡。下面是一个示例代码:
```python
fig, ax = plt.subplots()
ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual')
ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间')
ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间')
ax.set_title('Prediction Intervals')
ax.set_xlabel('Time')
ax.set_ylabel('RV_5min')
plt.legend()
plt.show()
```
这段代码将在同一幅图中绘制 `Actual` 曲线,并在其上方分别绘制了 `无ps置信区间` 和 `有ps置信区间` 的填充区域,它们不会相互遮挡。注意确保只创建一个 `fig` 和 `ax` 对象,并在同一个 `ax` 对象上调用多个 `fill_between` 方法。
matplotlib.fill_between给每一个图例加边框
如果您想要为每个图例都添加边框,可以使用`get_legend_handles_labels()`方法获取所有的图例句柄和标签,然后遍历它们并为每个图例设置边框。以下是一个示例代码:
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
fig, ax = plt.subplots()
region = ax.fill_between(x, y1, y2, alpha=0.5, label='Region')
line1, = ax.plot(x, y1, label='Sin(x)')
line2, = ax.plot(x, y2, label='Cos(x)')
handles, labels = ax.get_legend_handles_labels()
for handle in handles:
handle.set_edgecolor('black') # 设置每个图例的边框颜色为黑色
ax.legend(handles=handles, labels=labels) # 添加图例
plt.show()
```
在这个例子中,我们使用`get_legend_handles_labels()`方法获取所有的图例句柄和标签,然后遍历它们并为每个图例设置边框颜色为黑色。最后,我们使用`legend()`方法将所有的图例添加到图表中。