在增加边距后要删除matplotlib中的上轴和右轴? [英] Remove top and right axis in matplotlib after increasing margins?
问题描述
生成具有所需大小和边距的图形:
Generate figure with desired size and margins:
fig = plt.figure(figsize=(3,5))
ax = plt.Axes(fig,[left,bottom,width,height])
fig.add_axes(ax)
这连同图例、网格线和其他一切都给了我我所期待的,除了我无法删除顶部和右侧的轴.我在这里,将我定向到matplotlib 示例.
This along with legends, gridlines and everything else gives me what i'm expecting, except i can not remove the top and right axis. I referred to a similar question here, which directs me to a matplotlib example.
我尝试过
ax = Subplot(fig,111)
fig.add_subplot(ax)
ax.axis["top"].set_visible(False)
ax.axis["right"].set_visible(False)
stackoverflow 还不让我发图片,因为我没有足够的积分,所以我希望我的画足够了.
stackoverflow would not let me post a picture yet because i don't have enough points so i hope my drawing will suffice.
_________
| |
| |
| |
|_|
|_________
前面图的上轴和右轴被删除了(太好了!),但是我后面有第二个图,没有任何图.
The top and right axis of the front plot were removed (which is great!), but i have a second figure in the back that has nothing plotted.
我曾尝试查看 matplotlib 站点,但我仍然很难理解 add_axes() 和 add_subplot() 的作用.
I have tried looking at the matplotlib site, but i am still having a hard time understanding what exactly add_axes() and add_subplot() do.
推荐答案
这是一个解决方案,它展示了两种可能的方法来解决您的问题:
Here is a solution which shows two possible ways to solve your problem:
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()
这篇关于在增加边距后要删除matplotlib中的上轴和右轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!