简体   繁体   English

从matplotlib图表中删除边缘

[英]Remove edges from matplotlib chart

I have a question regarding removing edges from charts generated using matplotlib for Python. 我有一个关于从使用matplotlib for Python生成的图表中删除边的问题。 What I would like to create is horizontal bar chart where only bars can be seen. 我要创建的是水平条形图,其中只能看到条形图。 For generating chart I am using following code: 为了生成图表,我使用以下代码:

fig, ax = plt.subplots(figsize=(5, 5))
top_10.plot(kind="barh", ax=ax, color=["#dd4937"])
ax.axis("off")
ax.legend().set_visible(False)
ax.invert_yaxis()
ax.invert_xaxis()
ax.set_facecolor('red')
fig.savefig(template_dir + "/top_10_chart.png", transparent=True)

It almost creates why I require, but there is an space between edge of exported .png and first bar (picture below - added edge box just to show what I would like to achieve). 它几乎创建了我为什么要这样做的原因,但是导出的.png的边缘和第一个小节之间有一个空格(下图-添加的边缘框只是为了显示我想要实现的目标)。 What I would like to achieve is that first bar is on the edge of picture. 我要实现的是第一个小节位于图片的边缘。 I presume reason is that axes is nested in figure - can this be changed or for example remove axis (as currently they are only invisibe)? 我认为原因是轴嵌套在图形中-可以更改轴还是删除轴(因为当前它们仅是可见的)?

在此处输入图片说明

Thank you and best regards 感谢你并致以真诚的问候

Just posting answer here in case it comes in handy to someone. 只要在这里发布答案,以防有人使用。

As both ImportanceOfBeingErnest and IMCoins pointed out, there is actually an option to adjust figure. 正如ImportanceOfBeingErnest和IMCoins所指出的那样,实际上存在调整身高的选项。 Only thing that I have added to code is following line: 我添加到代码中的唯一内容是以下行:

fig.subplots_adjust(top=1, bottom=0)

Best regards 最好的祝福

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM