等高线图例-Matplotlib [英] Contour plot legend - Matplotlib
问题描述
正如问题所述,我有一个等高线图,我想显示一个if的图例.
As the question says, I have a contour plot and I would like show a legend for if.
我使用的等高线图样式使用:
I'm using the contour plot style that uses:
虚线行表示负级别
实心行表示正值值
我想为他们提供一个图例(虚线==负数和实心==正面).
I would like to have a legend for them (dashed == negative and solid == positive).
I tried the approaches found here and here. However, as can be seen below, this doesn't show the correct result.
# Draw the scalar field level curves
div_field = plt.contour(x, y, div_scalar_field, colors='white')
rot_field = plt.contour(x, y, rot_scalar_field, colors='lightgoldenrodyellow')
labels = ['Div Neg', 'Div Pos', 'Rot Neg', 'Rot Pos']
div_field.collections[0].set_label(labels[0])
div_field.collections[-1].set_label(labels[1])
rot_field.collections[0].set_label(labels[2])
rot_field.collections[-1].set_label(labels[3])
在 div标量字段中,我只有正电平,所以我得到了两个具有相同线条样式的标签.
As I for the div scalar field I just have positive levels, I got two labels with the same line style.
我想知道如何才能正确实现自己想要的目标.
I'm wondering how could I achieve what I want properly.
谢谢.
推荐答案
我可以手动设置图例来解决此问题(我不知道这是否是最好的方法):
I could solve this manually setting the legend (which I don't know if it's the best approach):
div_neg = plt.Line2D((0, 1), (0, 0), color='white', linestyle='--', linewidth=2)
div_pos = plt.Line2D((0, 1), (0, 0), color='white', linestyle='-', linewidth=2)
rot_neg = plt.Line2D((0, 1), (0, 0), color='lightgoldenrodyellow', linestyle='--', linewidth=2)
rot_pos = plt.Line2D((0, 1), (0, 0), color='lightgoldenrodyellow', linestyle='-', linewidth=2)
plt.legend([rot_max, div_neg, div_pos, rot_neg, rot_pos],
['Rot Max Points', 'Div Neg', 'Div Pos', 'Rot Neg', 'Rot Pos'])
这篇关于等高线图例-Matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!