Legend with vertical line in matplotlib

末鹿安然 提交于 2019-12-07 04:12:01

问题


I need to show a vertical line in a matplotlib legend for a specific reason. I am trying to make matplotlib understand that I want a vertical line with the lines.Line2D(x,y) but this is clearly not working.

import matplotlib.pyplot as plt
from matplotlib import lines
fig, ax = plt.subplots()
ax.plot([0,0],[0,3])
lgd = []
lgd.append(lines.Line2D([0,0],[0,1], color = 'blue', label = 'Vertical line'))
plt.legend(handles = lgd)

I need the line to appear vertical, not the legend. Can anyone help?


回答1:


You can use the vertical line marker when making your line2D object. A list of valid markers can be found here.

import matplotlib.pyplot as plt
from matplotlib import lines

fig, ax = plt.subplots()
ax.plot([0,0],[0,3])

vertical_line = lines.Line2D([], [], color='#1f77b4', marker='|', linestyle='None',
                          markersize=10, markeredgewidth=1.5, label='Vertical line')

plt.legend(handles = [vertical_line])

plt.show()




回答2:


Mark all lines vertically

If the aim is to get every line marked vertically instead of horizontally in the legend, you could update the legend handle via the handler_map.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerLine2D

plt.plot([1,3,2], label='something')
plt.plot([.5,.5], [1,3], label='something else')

def update_prop(handle, orig):
    handle.update_from(orig)
    x,y = handle.get_data()
    handle.set_data([np.mean(x)]*2, [0, 2*y[0]])

plt.legend(handler_map={plt.Line2D:HandlerLine2D(update_func=update_prop)})

plt.show()

Replicate line in miniature form

If the aim is to get a miniature version of the plotted line in the legend, you may in principle use this answer to Using a miniature version of the plotted data as the legend handle. There is a slight modification needed to account for a possibly 0 width bounding box though, which I now also edited into the original answer. Here, it would look like:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerLine2D
import matplotlib.path as mpath
from matplotlib.transforms import BboxTransformFrom, BboxTransformTo, Bbox


class HandlerMiniatureLine(HandlerLine2D):
    def create_artists(self, legend, orig_handle,
                       xdescent, ydescent, width, height, fontsize,
                       trans):

        legline, _ = HandlerLine2D.create_artists(self,legend, orig_handle,
                                xdescent, ydescent, width, height, fontsize, trans)

        legline.set_data(*orig_handle.get_data())

        ext = mpath.get_paths_extents([orig_handle.get_path()])
        if ext.width == 0:
            ext.x0 -= 0.1
            ext.x1 += 0.1
        bbox0 = BboxTransformFrom(ext)
        bbox1 = BboxTransformTo(Bbox.from_bounds(xdescent, ydescent, width, height))

        legline.set_transform(bbox0 + bbox1 + trans)
        return legline,


plt.plot([1,3,2], label='something')
plt.plot([.5,.5], [1,3], label='something else')

plt.legend(handler_map={plt.Line2D:HandlerMiniatureLine()})

plt.show()



来源:https://stackoverflow.com/questions/53122592/legend-with-vertical-line-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!