等高线图的传说 - Matplotlib

问题描述 投票:1回答:2

因为这个问题说,我有一个等高线图,我想显示,如果一个传奇。

我使用的是采用等高线图的风格:

虚线为负的水平

实线为正值

我想对他们有一个传说(虚线==负和固体==阳性)。

我试图找到herehere的途径。然而,正如下面可以看到,这并没有显示正确的结果。

# 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标量场,我只是有积极的水平,我有两个标签具有相同的线条样式。

我想知道我怎么能实现我想要正常。

先感谢您。

python matplotlib plot legend contour
2个回答
1
投票

我可以解决这个手动设置的传说(我不知道这是否是最好的方法):

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'])


0
投票

像下面这样对我的作品的东西 - 这完全是黑客使用标记的虚拟点,获取它的颜色,它应用到轮廓,然后就绘制以通常的方式传说:

    import matplotlib as plt

    labels = ['div_field'] # etc.

    dummy_position = [-1.0e3,-1.0e3] # Could automate

    colors = []
    for k in labels:

        # Fetch colours via a dummy point
        dummy_point = plt.plot(dummy_position[0],dummy_position[1], label = k)
        c = dummy_point[-1].get_color()
        colors.append(c)

        # This is specific to your problem, but roughly:
        div_field = plt.contour(x, y, div_scalar_field, colors=c)
        # etc.

    _=plt.legend()

    plt.savefig('contours.pdf')

希望是有道理的。

© www.soinside.com 2019 - 2024. All rights reserved.