为了去掉图中的框架,我写了
frameon=False
与pyplot完美结合。图,但与matplotlib。图只移除了灰色背景,帧保持不变。此外,我只想显示线条,其余的图形都是透明的。
用pyplot我可以做我想做的事情,我想用matplotlib做一些很长的原因,我宁愿不提扩展我的问题。
为了去掉图中的框架,我写了
frameon=False
与pyplot完美结合。图,但与matplotlib。图只移除了灰色背景,帧保持不变。此外,我只想显示线条,其余的图形都是透明的。
用pyplot我可以做我想做的事情,我想用matplotlib做一些很长的原因,我宁愿不提扩展我的问题。
当前回答
问题
我在使用坐标轴时遇到了类似的问题。类参数是frameon,但kwarg是frame_on。axes_api > > > plt.gca()这里(frameon = False) AttributeError:未知属性帧
解决方案
frame_on
例子
data = range(100)
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(data)
#ax.set(frameon=False) # Old
ax.set(frame_on=False) # New
plt.show()
其他回答
问题
我在使用坐标轴时遇到了类似的问题。类参数是frameon,但kwarg是frame_on。axes_api > > > plt.gca()这里(frameon = False) AttributeError:未知属性帧
解决方案
frame_on
例子
data = range(100)
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(data)
#ax.set(frameon=False) # Old
ax.set(frame_on=False) # New
plt.show()
plt.box(False)
plt.xticks([])
plt.yticks([])
plt.savefig('fig.png')
应该能行。
在@peeol的出色回答的基础上,您还可以通过以下操作删除框架
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()
在新版本的matplotlib中,摆脱丑陋帧的最简单方法是:
import matplotlib.pyplot as plt
plt.box(False)
如果你真的必须总是使用面向对象的方法,那么执行:ax.set_frame_on(False)。
plt.axis('off')
plt.savefig(file_path, bbox_inches="tight", pad_inches = 0)
plt。Savefig本身有这些选项,只需要在之前设置轴