我想绘制数据,其中连续的点由矩形的部分连接,或者是平坦的然后垂直的,或者垂直的然后平坦的。这是一个天真的方法:
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()
Run Code Online (Sandbox Code Playgroud)
有没有任何 pyplot 选项可以实现这一点?numpy 或 scipy 中内置插值功能?我在文档中没有看到任何内容(例如,这不是箱形图或条形图,而是不同的)
我可以编写一个简单的函数来为我进行这种类型的插值,但如果可能的话,我宁愿坚持使用库的东西。
您可以使用plt.step以下方法获得相同的结果:
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2879 次 |
| 最近记录: |