使用 numpy,您可以使用逻辑索引。
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.array([10000, 1000, 100, 10, 1, 5, 50, 500, 5000, 50000])
y = np.array([-10000, -1000, -100, -10, -1, 5, 50, 500, 5000, 50000])
ax.plot(x,abs(y),'+-b',label='all data')
ax.plot(abs(x[y<= 0]),abs(y[y<= 0]),'o',markerfacecolor='none',
markeredgecolor='r',
label='we are negative')
ax.set_xscale('log')
ax.set_yscale('log')
ax.legend(loc=0)
plt.show()
关键特性是首先绘制所有绝对的y-values,然后将那些原本为负的值重新绘制为空心圆圈以将它们单独列出。第二步使用逻辑索引x[y<=0] 和y[y<=0] 仅选择y-array 中那些为负的元素。
上面的例子给你这个图:
如果你真的有两个不同的数据集,下面的代码会给你和上面一样的图:
x1 = np.array([1, 10, 100, 1000, 10000])
x2 = np.array([5, 50, 500, 5000, 50000])
y1 = np.array([-1, -10, -100, -1000, -10000])
y2 = np.array([5, 50, 500, 5000, 50000])
x = np.concatenate((x1,x2))
y = np.concatenate((y1,y2))
sorted = np.argsort(y)
ax.plot(x[sorted],abs(y[sorted]),'+-b',label='all data')
ax.plot(abs(x[y<= 0]),abs(y[y<= 0]),'o',markerfacecolor='none',
markeredgecolor='r',
label='we are negative')
在这里,您首先使用np.concatenate 组合x- 和y- 数组。然后,您使用np.argsort 对y-array 进行排序,以确保您在绘图时不会得到过于曲折的线。当您调用第一个图时,您使用该索引数组 (sorted)。由于第二个图只绘制符号而不绘制连接线,因此此处不需要排序数组。