绘制/标记1D阵列中的选定点

时间:2015-01-28 22:57:18

标签: python matplotlib

这似乎是一个简单的问题,但我已经尝试了很长时间了。 我得到了一个1d数组数据(命名为' hightemp_unlocked',在找到峰值(峰值所在的位置数组)后,我想在图上标出峰值。

import matplotlib
from matplotlib import pyplot as plt

.......

plt.plot([x for x in range(len(hightemp_unlocked))],hightemp_unlocked,label='200 mk db ramp')
plt.scatter(peaks, hightemp_unlocked[x in peaks], marker='x', color='y', s=40)

由于某种原因,它一直告诉我x,y必须是相同的大小 它显示:

 File "period.py", line 86, in <module>
    plt.scatter(peaks, hightemp_unlocked[x in peaks], marker='x', color='y', s=40)
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/pyplot.py", line 2548, in scatter
    ret = ax.scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, faceted, verts, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/axes.py", line 5738, in scatter
    raise ValueError("x and y must be the same size")

2 个答案:

答案 0 :(得分:1)

你几乎走在正确的轨道上,但hightemp_unlocked[x in peaks]并不是你想要的。怎么样:

from matplotlib import pyplot as plt

# dummy temperatures
temps = [10, 11, 14, 12, 10, 8, 5, 7, 10, 12, 15, 13, 12, 11, 10]

# list of x-values for plotting
xvals = list(range(len(temps)))

# say our peaks are at indices 2 and 10 (temps of 14 and 15)
peak_idx = [2, 10]

# make a new list of just the peak temp values
peak_temps = [temps[i] for i in peak_idx]

# repeat for x-values
peak_xvals = [xvals[i] for i in peak_idx]

# now we can plot the temps
plt.plot(xvals, temps)

# and add the scatter points for the peak values
plt.scatter(peak_xvals, peak_temps)

答案 1 :(得分:1)

我认为hightemp_unlocked[x in peaks]不是你想要的。此处x in peaks读取条件语句“x中的peaks?”并将返回TrueFalse,具体取决于x中最后存储的内容。解析hightemp_unlocked[x in peaks]时,TrueFalse被解释为0或1,它只返回hightemp_unlocked的第一个或第二个元素。这解释了数组大小错误。

如果peaks是一个索引数组,那么只需hightemp_unlocked[peaks]将返回相应的值。