Python 在matplotlib中使用矩形插值绘制直线
我想要绘制数据,其中连续的点由矩形的部分连接,要么是平的,然后是垂直的,要么是垂直的,然后是平的。下面是一个简单的方法:Python 在matplotlib中使用矩形插值绘制直线,python,matplotlib,Python,Matplotlib,我想要绘制数据,其中连续的点由矩形的部分连接,要么是平的,然后是垂直的,要么是垂直的,然后是平的。下面是一个简单的方法: import matplotlib.pyplot as plt x_data = [0.0, 1.0, 3.0, 4.5, 7.0] y_data = [1.5, 3.5, 6.0, 2.0, 9.0] # Linear interpolation plt.plot(x_data, y_data, label='linear_interp') # Vertical fi
import matplotlib.pyplot as plt
x_data = [0.0, 1.0, 3.0, 4.5, 7.0]
y_data = [1.5, 3.5, 6.0, 2.0, 9.0]
# Linear interpolation
plt.plot(x_data, y_data, label='linear_interp')
# Vertical first interpolation
x_data_vert_first = [0.0, 0.0, 1.0, 1.0, 3.0, 3.0, 4.5, 4.5, 7.0]
y_data_vert_first = [1.5, 3.5, 3.5, 6.0, 6.0, 2.0, 2.0, 9.0, 9.0]
plt.plot(x_data_vert_first, y_data_vert_first, label="vert_first")
# Horizontal first interpolation
x_data_flat_first = [0.0, 1.0, 1.0, 3.0, 3.0, 4.5, 4.5, 7.0, 7.0]
y_data_flat_first = [1.5, 1.5, 3.5, 3.5, 6.0, 6.0, 2.0, 2.0, 9.0]
plt.plot(x_data_flat_first, y_data_flat_first, label="flat_first")
plt.legend(loc='upper left')
plt.show()
有任何pyplot选项可以实现这一点吗?numpy或scipy中的内置插值功能?我在文档中没有看到任何内容(例如,这不是方框图或条形图,而是不同的)
我可以编写一个简单的函数来完成这种插值,但如果可能的话,我更愿意使用库中的内容。您可以使用它来获得相同的结果:
plt.plot(x_data, y_data, label='linear_interp')
plt.step(x_data, y_data, where = 'pre', label = 'vert_first')
plt.step(x_data, y_data, where = 'post', label = 'flat_first')
plt.legend(loc='upper left')
plt.show()
这就是我错过的,谢谢!