Python - 从图表和图例中删除边框

标签 python matplotlib

我有以下情节:

dfA.plot.bar(stacked=True, color=[colorDict.get(x, '#333333') for x in 
dfA.columns],figsize=(10,8))
plt.legend(loc='upper right', bbox_to_anchor=(1.4, 1))

显示这个:

enter image description here

我想删除图表和图例的所有边框,即图表周围的框(保留轴号,如 2015 和 6000 等)

我找到的所有示例都涉及 spine 和“ax”,但是我没有使用 fig = plt.figure() 等构建我的图表。

有人知道怎么做吗?

最佳答案

您可以通过在调用 plt.legend() 时使用参数 frameon=False 来移除图例的边框。

如果只有一个图形和坐标轴处于事件状态,则可以使用 plt.gca() 获取当前坐标轴。或者 df.plot.bar 返回一个 axes 对象(我建议使用它,因为 plt.gca() 在处理多个对象时可能会混淆人物)。因此,您可以将书脊的可见性设置为 False:

ax = dfA.plot.bar(stacked=True, color=[colorDict.get(x, '#333333') for x in 
dfA.columns],figsize=(10,8))
plt.legend(loc='upper right', bbox_to_anchor=(1.4, 1), frameon=False)

for spine in ax.spines:
    ax.spines[spine].set_visible(False)

    # Color of the spines can also be set to none, suggested in the comments by ScoutEU 
    # ax.spines[spine].set_color("None")

关于Python - 从图表和图例中删除边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50019758/

相关文章:

Python BeautifulSoup 在 CSV 中打印信息

python - SqlAlchemy排序问题

python - Plotly:如何向烛台图表添加交易量

python - 在 Synology 上安装 python 模块 - pip 错误

python - 将 batchnorm(TensorFlow) 的 is_training 变为 False

python - 使用掩码计算DataFrame中的平均值

python - 根据pygame中的球运动画一条线

python - 在 Python 中将 4D 数据绘制为分层热图

python - 带有圆圈表示人口规模的热图

python - matplotlib:3D 图中的网格