2024-04-29 15:32:19 发布
网友
我有两组数据,它们在图形中显示为直线。如何填充它们之间的颜色区域?在
import matplotlib.pyplot as plt curve1, = plt.plot(xdata, ydata) curve2, = plt.plot(xdata, ydata)
我试过了:
谢谢你
必须使用ydata作为^{}的参数,而不是曲线。在
ydata
要么直接使用ydata,要么从curve1/2对象中获取它们,比如ydata=curve1.get_ydata()。在
curve1/2
ydata=curve1.get_ydata()
下面是一个改编自docs的示例:
import matplotlib.pyplot as plt import numpy as np x = np.arange(-5, 5, 0.01) y1 = -5*x*x + x + 10 y2 = 5*x*x + x c1, = plt.plot(x, y1, color='black') c2, = plt.plot(x, y2, color='black') # If you want/have to get the data form the plots # x = c1.get_xdata() # y1 = c1.get_ydata() # y2 = c2.get_ydata() plt.fill_between(x, y1, y2, where=y2 >y1, facecolor='yellow', alpha=0.5) plt.fill_between(x, y1, y2, where=y2 <=y1, facecolor='red', alpha=0.5) plt.title('Fill Between') plt.show()
最后你会得到:
必须使用} 的参数,而不是曲线。在
ydata
作为^{要么直接使用
ydata
,要么从curve1/2
对象中获取它们,比如ydata=curve1.get_ydata()
。在下面是一个改编自docs的示例:
最后你会得到:
相关问题 更多 >
编程相关推荐