python - 重命名子图中的图例 : matplotlib

标签 python matplotlib

我有一个数据框stats,其中包含两列OldNew。我将它们绘制在两个单独的子图中,但我想重命名我的图例,但保留字幕不变。 plt.legend(labels= ['Stats']) 仅重命名一个子图图例。将感谢您的帮助。

stats.plot(kind='bar',
               grid=False, subplots=True, 
               figsize=(20,10), fontsize=16, 
               color='#2E9240', 
               sharex=True, sharey=True)

plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom='off',      # ticks along the bottom edge are off
    top='off',         # ticks along the top edge are off
    labelbottom='off') # labels along the bottom edge are off

plt.legend(labels= ['Stats'])

plt.show()

enter image description here

最佳答案

DataFrame.plot返回每个子图的绘图轴或轴数组。使用轴来更改其图例。

axes = stats.plot(kind='bar',
                  grid=False, subplots=True, 
                  figsize=(20,10), fontsize=16, 
                  color='#2E9240', 
                  sharex=True, sharey=True)
for ax in axes:
    ax.legend(['Stats'])

关于python - 重命名子图中的图例 : matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47022575/

相关文章:

python 向变量添加额外值

python - AWS Lambda "Unable to import module ' 处理程序' : No module named handler"

python - 使用 Pandas 重新采样移位间隔

python - matplotlib navtoolbar 在 wx 2.9 (Mac OS X) 中没有实现

python - slider 不会更改 3D 绘图中的值

来自 fileconfig 的 Python 自定义日志处理程序

python - 用于设置 xtics 的 mpld3 插件

numpy - 使用来自 matplotlib 的 plt.plot() 将所有 numpy 数组绘图点相互连接

python - 带有表示频率的单独列表的直方图

python - Pyspark错误+方法__getnewargs__([])不存在