-1

使用茎,我想显示 (x,y) 值,所有 y 值都使用红色,其余的使用绿色。正是我想用红色显示从公差到最大值,用绿色显示从公差到零。在最终显示中,我希望看到所有图表都低于公差绿色,高于公差红色。

4

1 回答 1

2

这是一个完成这项工作的技巧。我为 x 和 y 创建了任意数据,容差为 2.0:

import numpy as np
import matplotlib.pyplot as plt
plt.ion()

x = np.arange(1., 10.)
y = x**.5
tolerance = 2.

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

ax.stem(x[y<=tolerance], y[y<=tolerance], 'g', zorder=10)
if len(x[y>tolerance]) > 0:
    ax.stem(x[y>tolerance], y[y>tolerance], 'r', zorder=10)
    ax.vlines(x[y>tolerance], 0, tolerance, 'g', zorder=20)

ax.axhline(tolerance)

ax.axhline不是必需的,但我添加它是为了显示公差的值。此代码的结果如下所示:

代码输出

于 2016-02-13T00:49:16.043 回答