matplotlib中的两点线段图

2024-05-19 15:06:20 发布

您现在位置:Python中文网/ 问答频道 /正文

如何绘制两点线段图,如下图所示 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值总是成对的,因此我需要一条实线来连接这些等效值,然后在这对和下一对之间加一条虚线。在


Tags: 数据绘制虚线线段等效值实线
2条回答

你的意思是这样吗?在

import pylab
xdata = [0, 1, 2, 3, 4, 5]
ydata = [0, 1, 2, 2, 1, 0]
# Assuming xdata, ydata are of suitable length and type
plots = [pylab.plot(xdata[i:i + 2], ydata[i:i + 2], **whatever_keyword_arguments) for i in xrange(0, len(xdata), 2)]
pylab.show()

操作编辑后编辑:

我明白你的意思了,用破折号加几行很简单。在

^{pr2}$

这是否达到了你的期望?在

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()

相关问题 更多 >