我有以下情节:
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))
显示这个:
我想删除图表和图例的所有边框,即图表周围的框(保留轴号,如 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/