如何在Matplotlib中水平显示图例元素? 要水平显示图例元素,我们可以采取以下步骤 设置图形大小并调整子图之间和周围的填充。 使用 plot() 方法,绘制具有标签 line1,line2 和 line3 的线条。 使用 legend() 方法在图形上放置图例,使用ncol值的标签数作为参数。 要显示图形,请使用 show() 方法。 示例 from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True line1,= plt.plot([1,2,3],label ="line1") line2,= plt.plot([3,2,1],label ="line2") line3,= plt.plot([2,3,1],label ="line3") plt.legend(ncol = 3,loc ="upper right") plt.show() PythonCopy 输出