python - Matplotlib - 不同时刻特定轮廓的图例

标签 python numpy matplotlib contour

我正在尝试在不同时间绘制具有特定等高线(级别 = 320)的图形,这就是使用循环的原因。 我想绘制一个带有标签的图例来指定循环期间的时间,如下所示:

enter image description here

显示了我的部分代码:

cmap = plt.cm.hot

instant = 0
for instant in range(0,Sigma_stockMAX.shape[2]):

    name = 'test'

    VM_MAX_eq = 1./np.sqrt(2)*np.sqrt((Sigma_stockMAX[:,2,instant]-Sigma_stockMAX[:,3,instant])**2 + (Sigma_stockMAX[:,3,instant])**2 + (-Sigma_stockMAX[:,2,instant])**2 + 6*Sigma_stockMAX[:,5,instant]**2)


    VM_MAX_eq_interpolate = interpolate(VM_MAX_eq, vtx, wts).reshape(X.shape[0],X.shape[0])

    plt.legend(loc='upper center', shadow=True)

    contour = plt.contour(XX_field[20:480,20:480], YY_field[20:480,20:480],ndimage.filters.gaussian_filter(VM_MAX_eq_interpolate[20:480,20:480], 5),colors=(cmap(instant/ np.float(Sigma_stockMAX.shape[2])),),levels = [320],linestyles=('-',),linewidths=(2,))

plt.savefig(name+ '_0' + test[instant][81:110] + ".png", dpi=250)

我试图在循环中添加这部分但它不起作用:

labels = ['line1', 'line2','line3','line4',
           'line5', 'line6', 'line6', 'line6', 'line6', 'line6']

for i in range(len(labels)):
    contour.collections[instant].set_label(labels[instant])

最佳答案

我用过:http://matplotlib.org/examples/pylab_examples/contour_demo.html

cmap = plt.cm.hot
lines = []
labels = []
i = 0
instant = 0
for instant in range(0,Sigma_stockMAX.shape[2]):

    name = 'test'

    VM_MAX_eq = 1./np.sqrt(2)*np.sqrt((Sigma_stockMAX[:,2,instant]-Sigma_stockMAX[:,3,instant])**2 + (Sigma_stockMAX[:,3,instant])**2 + (-Sigma_stockMAX[:,2,instant])**2 + 6*Sigma_stockMAX[:,5,instant]**2)

    VM_MAX_eq_interpolate = interpolate(VM_MAX_eq, vtx, wts).reshape(X.shape[0],X.shape[0])

    contour = plt.contour(XX_field[20:480,20:480], YY_field[20:480,20:480],ndimage.filters.gaussian_filter(VM_MAX_eq_interpolate[20:480,20:480], 5),colors=(cmap(instant/ np.float(Sigma_stockMAX.shape[2])),),levels = [320],linestyles=('-',),linewidths=(2,))
    lines.extend(contour.collections)
    labels.extend(['line'+str(i+j) for j in range(len(contour.collections))])
    i += len(contour.collections)
plt.legend(lines, labels, loc='upper center', shadow=True)
plt.savefig(name+ '_0' + test[instant][81:110] + ".png", dpi=250)

关于python - Matplotlib - 不同时刻特定轮廓的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39002125/

相关文章:

python - 在 Python 中验证文本文件中的用户名和密码

python - 对 2D NumPy 数组中的非零值进行聚类

linux - 导入错误 : No module named matplotlib with matplotlib installed

Python:将表达式解释为文本而不计算它

python - 使用 PyInstaller 转换为 .app 的 Tkinter 程序立即关闭

python - 在 x、y 和 z 中具有不同间隔的定期采样 3D 数据的快速插值

python - 使用 matplotlib.pyplot (Python) 绘制椭圆

python - 将轴尺寸限制为另一个轴的尺寸

python - 在 Pandas Data Frame 列字符串中为每个字符插入空格

Python 纯 RMSE 与 Sklearn