python - matplotlib 中的两点线段图

标签 python matplotlib plot

如何绘制如下图所示的两点线段图 enter image description here

数据如下

x = [1,2,3,4,5,6]

y = [1.2,1.2,-2.1, -2.1, 4.1, -4.1] #这些 y 值总是成对的,所以我需要一条实线来连接这些等效值,然后在这一对和下一对。

最佳答案

这是否实现了您的期望?

import numpy as np
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 6]
y = [1.2, 1.2, 2.1, 2.1, -4.1, -4.1]

plt.plot(x, y, 'm--')

pair_x_array = np.reshape(x, (-1, 2))
pair_y_array = np.reshape(y, (-1, 2))
for i, pair_x in enumerate(pair_x_array):
    pair_y = pair_y_array[i]
    plt.plot(pair_x, pair_y, 'm', linewidth=3)

plt.show()

关于python - matplotlib 中的两点线段图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32917006/

相关文章:

python - 使用 scrapy 我得到一个空项目

php - Python 相当于 php 包

numpy - 如何获得 0 和 1 之间的 sigmoid 函数以获得正确答案的概率?

r - 使用任意数据点和视点在 R 中生成 3D 图

sql - 从 Postgresql 表中绘制表和关系

r - 使用 ggplot 绘制函数,相当于 curve()

python - opencv中如何去除图像中的眩光?

python - 如何使用 for 循环对一组数据帧运行操作?

python - 如何实时更新 NetworkX 图?

matplotlib - 使用 Matplotlib imshow 的选择性模式