在增加边距后如何去掉matplotlib的顶部和右侧坐标轴?
生成一个指定大小和边距的图形:
fig = plt.figure(figsize=(3,5))
ax = plt.Axes(fig,[left,bottom,width,height])
fig.add_axes(ax)
这个设置加上图例、网格线和其他所有元素,给了我想要的效果,除了我无法去掉顶部和右侧的坐标轴。我参考了一个类似的问题,在这里,它引导我去看一个matplotlib的示例。
我尝试了
ax = Subplot(fig,111)
fig.add_subplot(ax)
ax.axis["top"].set_visible(False)
ax.axis["right"].set_visible(False)
因为我还没有足够的积分,stackoverflow不让我发图片,所以希望我的画图能说明问题。
_________
| |
| |
| |
|_|
|_________
前面的图的顶部和右侧坐标轴被去掉了(这太好了!),但是我后面还有一个图形,里面什么都没有画。
我试着查看matplotlib的网站,但我还是很难理解add_axes()和add_subplot()到底是干什么的。
1 个回答
3
这里有一个解决方案,展示了两种可能的方法来解决你的问题:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.axislines import Subplot
left,bottom,width,height= -0.02 , 0.12, 1, 0.9
fig = plt.figure(figsize=(3,5))
ax1 = plt.Axes(fig,[left,bottom,width,height])
ax1.plot([1,2,3,4],'b') # plot on the first axes you created
fig.add_axes(ax1)
# using subplot you are acually using higher level objects
ax2 = Subplot(fig,111) # this addes another axis instance
fig.add_subplot(ax2)
ax2.axis["top"].set_visible(False)
ax2.axis["right"].set_visible(False)
ax2.plot([1,2,3,4,5],'r') # thos plots on the second
# now comment everything in ax2, and uncomment ax3
# you will get a crude, low level control of axes
# but both do what you want...
#ax3 = plt.Axes(fig,[left+0.2,bottom-0.2,width,height])
#ax3.plot([1,2,3,4],'g') # plot on the first axes you created
#for loc, spine in ax3.spines.iteritems():
# if loc in ['left','bottom']:
# spine.set_position(('outward',10)) # outward by 10 points
# if loc in ['right','top']:
# spine.set_color('none') # don't draw spine
#fig.add_axes(ax3)
plt.show()