在@peeol's excellent answer 的基础上,您也可以通过以下方式移除框架
for spine in plt.gca().spines.values():
spine.set_visible(False)
举个例子(整个代码示例可以在这篇文章的末尾找到),假设你有一个这样的条形图,
您可以使用上述命令删除框架,然后保留 x- 和 ytick 标签(图未显示)或将它们删除
plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on')
在这种情况下,可以直接标记条形;最终的情节可能是这样的(代码可以在下面找到):
这是生成绘图所需的全部代码:
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
xvals = list('ABCDE')
yvals = np.array(range(1, 6))
position = np.arange(len(xvals))
mybars = plt.bar(position, yvals, align='center', linewidth=0)
plt.xticks(position, xvals)
plt.title('My great data')
# plt.show()
# get rid of the frame
for spine in plt.gca().spines.values():
spine.set_visible(False)
# plt.show()
# remove all the ticks and directly label each bar with respective value
plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on')
# plt.show()
# direct label each bar with Y axis values
for bari in mybars:
height = bari.get_height()
plt.gca().text(bari.get_x() + bari.get_width()/2, bari.get_height()-0.2, str(int(height)),
ha='center', color='white', fontsize=15)
plt.show()