Matplotlib:如何水平显示图例元素?

13 浏览
0 Comments

Matplotlib:如何水平显示图例元素?

我想将图例设置为水平显示。我并不是指类似于Matplotlib legend vertical rotation中描述的图例文本。我的实际情况包括使用小部件指定的任意数量的系列。但以下示例代表了这个挑战的要点:

代码片段:

# 导入
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# 数据
np.random.seed(123)
x = pd.Series(np.random.randn(100),index=pd.date_range('1/1/2000', periods=100)).cumsum()
y = pd.Series(np.random.randn(100),index=pd.date_range('1/1/2000', periods=100)).cumsum()
z = pd.Series(np.random.randn(100),index=pd.date_range('1/1/2000', periods=100)).cumsum()
df = pd.concat([x,y,z], axis = 1)
# 绘图
ax = df.plot()
plt.legend(loc="lower left")
plt.show()

图:

enter image description here

默认布局似乎是垂直的。

查看help(ax.legend)文档的详细信息,似乎没有一种直接的方法可以将其改为水平。或者有吗?


编辑 - 期望的图例:(使用 MS Paint)

enter image description here

0