本文介绍了填充python中两条曲线之间的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对绘制的两条曲线之间的区域进行着色.这是我绘制的.
I am trying to shade the area between two curves that I have plotted.This is what I plotted.
使用以下代码.
plt.scatter(z1,y1, s = 0.5, color = 'blue')
plt.scatter(z2,y2, s = 0.5, color = 'orange')
我尝试使用 plt.fill_between()
但要使其正常工作,我需要在 x_axis
上拥有相同的数据(需要执行类似 plt.fill_between(x,y1,y2)
).还有其他功能可能对此有帮助吗?还是我只是错误地使用 fill_between
.
I tried using plt.fill_between()
but for this to work I need to have the same data on the x_axis
(would need to do something like plt.fill_between(x,y1,y2)
).Is there any other function that might help with this or am I just using fill_between
wrong.
推荐答案
你可以试试:
plt.fill(np.append(z1, z2[::-1]), np.append(y1, y2[::-1]), 'lightgrey')
例如:
import numpy as np
import matplotlib.pyplot as plt
x1 = np.array([1,2,3])
y1 = np.array([2,3,4])
x2 = np.array([2,3,4,5,6])
y2 = np.array([1,2,3,4,5])
# plt.plot(x1, y1, 'o')
# plt.plot(x2, y2, 'x')
plt.scatter(x1, y1, s = 0.5, color = 'blue')
plt.scatter(x2, y2, s = 0.5, color = 'orange')
plt.fill(np.append(x1, x2[::-1]), np.append(y1, y2[::-1]), 'lightgrey')
plt.show()
这篇关于填充python中两条曲线之间的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!