Python 如何将图例添加到matplotlib eventplot?

Python 如何将图例添加到matplotlib eventplot?,python,matplotlib,Python,Matplotlib,有人知道如何将图例添加到matplotlib的新打印类型eventplot中吗 我尝试了很多事情都没有成功,包括: labels1 = ['red', 'green', 'blue', 'yellow', 'magenta', 'cyan'] ax2.eventplot(data1, colors=colors1, lineoffsets=lineoffsets1, linelengths=linelengths1, orientation='vertical', l

有人知道如何将图例添加到matplotlib的新打印类型eventplot中吗

我尝试了很多事情都没有成功,包括:

labels1 = ['red', 'green', 'blue', 'yellow', 'magenta', 'cyan']
ax2.eventplot(data1, colors=colors1, lineoffsets=lineoffsets1,
              linelengths=linelengths1, orientation='vertical', label=labels1)
ax2.legend()
这些编辑是在示例代码eventplot_demo.py上进行的,该代码位于

TL;博士 只是,
ax.legend(名称列表)

说明 从你的代码

labels1 = ['red', 'green', 'blue', 'yellow', 'magenta', 'cyan']
ax2.eventplot(data1, colors=colors1, lineoffsets=lineoffsets1,
              linelengths=linelengths1, orientation='vertical')
ax2.legend( labels1 )
例如

最终结果

最好在您的问题中提供可运行的示例(设置轴并包含伪数据和所有打印参数)。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib

# Fixing random state for reproducibility
np.random.seed(19680801)

# create random data
data = np.random.random([6, 50])

# set different colors for each set of positions
colors = np.array([[1, 0, 0],
                    [0, 1, 0],
                    [0, 0, 1],
                    [0, 0, 0],
                    [1, 0, 1],
                    [0, 1, 1]])
# label each set
labels = ['John','Mike','Bob','Sam','Alex','Eva']

fig, axs = plt.subplots(1,1)

# create a horizontal plot
axs.eventplot(data,color=colors)
# Just needed axs.legend(labels) the everything else is for the positioning
axs.legend(labels, bbox_to_anchor=(0., 1.0, 1., .10), loc=3,ncol=3, mode="expand", borderaxespad=0.)
plt.show()