Python 等高线图图例-Matplotlib
正如问题所说,我有一个等高线图,我想为if显示一个图例 我使用的等高线打印样式使用: 负级别的虚线 实线用于正值的线 我想为它们创建一个图例(虚线==负数,实线==正数) 我尝试了找到的方法,并找到了答案。但是,从下面可以看出,这并没有显示正确的结果Python 等高线图图例-Matplotlib,python,matplotlib,plot,legend,contour,Python,Matplotlib,Plot,Legend,Contour,正如问题所说,我有一个等高线图,我想为if显示一个图例 我使用的等高线打印样式使用: 负级别的虚线 实线用于正值的线 我想为它们创建一个图例(虚线==负数,实线==正数) 我尝试了找到的方法,并找到了答案。但是,从下面可以看出,这并没有显示正确的结果 # Draw the scalar field level curves div_field = plt.contour(x, y, div_scalar_field, colors='white') rot_field = plt.contour
# 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 scalar字段,我只有正值,我得到了两个具有相同线条样式的标签
我想知道我怎样才能实现我想要的
提前谢谢。我可以手动设置图例来解决这个问题(我不知道这是否是最好的方法):
以下类似的方法对我很有用-这一完整的方法是使用一个带标签的虚拟点,提取其颜色,将其应用于轮廓,然后以通常的方式绘制图例:
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')
希望这是有道理的
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')