【问题标题】:Remove top and right axis in matplotlib after increasing margins?增加边距后删除matplotlib中的上轴和右轴?
【发布时间】:2011-12-31 20:45:44
【问题描述】:

生成具有所需大小和边距的图形:

fig = plt.figure(figsize=(3,5))
ax = plt.Axes(fig,[left,bottom,width,height])
fig.add_axes(ax)

这连同图例、网格线和其他一切都给了我我所期望的,除了我不能删除顶部和右侧的轴。我提到了一个类似的问题here,它把我引向了一个matplotlib example

我试过了

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() 到底是做什么的。

【问题讨论】:

  • 你是否关闭了两个命令块之间的图形?如果你让它保持打开状态,matplotlib 默认会在你当前的图形上绘制而不清除它。
  • 我不这么认为。你需要做什么来确保这个数字是封闭的?
  • 我通常只是关闭绘图窗口。
  • 哦哈哈,是的,我关闭了它。谢谢

标签: python matplotlib


【解决方案1】:

这是一个解决方案,它显示了解决您的问题的两种可能方法:

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()

【讨论】:

  • 您帖子中的 for 循环正是我所需要的。我试图证明它是有用的,但我必须有 15 个代表。谢谢 Oz123
猜你喜欢
  • 2010-10-29
  • 1970-01-01
  • 1970-01-01
  • 2019-03-02
  • 2013-05-05
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多