有谁知道如何向 matplotlib 的新绘图类型 eventplot 添加图例?
我尝试过很多失败的事情,包括:
labels1 = ['red', 'green', 'blue', 'yellow', 'magenta', 'cyan']
ax2.eventplot(data1, colors=colors1, lineoffsets=lineoffsets1,
linelengths=linelengths1, orientation='vertical', label=labels1)
ax2.legend()
Run Code Online (Sandbox Code Playgroud)
这些编辑是对示例代码进行的,eventplot_demo.py 位于 http://matplotlib.org/dev/api/pyplot_api.html#matplotlib.pyplot.eventplot
只是,ax.legend( list_of_names )
从你的代码来看,
labels1 = ['red', 'green', 'blue', 'yellow', 'magenta', 'cyan']
ax2.eventplot(data1, colors=colors1, lineoffsets=lineoffsets1,
linelengths=linelengths1, orientation='vertical')
ax2.legend( labels1 )
Run Code Online (Sandbox Code Playgroud)
对于matplotlib.pyplot.eventplot中的示例
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2606 次 |
| 最近记录: |